2 responses to “Don’t use vars inside singleton objects”

  1. Maxime Lévesque

    I would like to mention that the the two vars that you have spotted are part of
    a bootstraping mechanism who’s only purpose is to allow this syntax :

    transaction {
    … your squeryl code here ….
    }

    (in addition to allowing session creation code supplied by user code
    of course …it’s an abstract factory)

    This is a bootstrapping mechanism, i.e. the kind of code that you set right after having read
    the configuration, most likely at VM start time.

    This mechanism is actually optionnal, you can use this alternative syntax :

    val session = new Session(…)

    using(session) {
    …your Squeryl code here …
    }

    All the issues you’ve mentioned won’t apply if you use this syntax,
    because you won’t be using the statically overridable factory.

    Scala is an “impure” functional language, i.e. immutability is strongly recommented
    but not enforced, the rational is that there will be a few cases where
    mutability is just more practical. Of course there must be a justification for introducing
    the mutability. My justification in this case is that in the predominant use cases
    need only one Session factory per VM, in these cases, there is just no point in setting it
    more than once, if someone needs to do this, they should be using sessions explicitely
    i.e. the using(session) {} syntax.

    You will notice that practically all Squeryl objects (Query[A], Table[A], Schema, etc…)
    are immutable, in fact they are meant to be shareable.