Reputation: 18148
We have several traits / classes / case classes with vals that are initialized using a database session (we're using Slick on top of MySql, so all Sessions
below have type scala.slick.session.Session
). The method that is initializing the trait will already have an open database session, and so I'd like to be able to pass this into the trait to be used on initialization; however, once the trait is initialized the session will be automatically closed, and so I don't want the trait to hold on to a reference to the closed session in order to eliminate the possibility that we'll accidentally try to use it.
trait MyTrait {
private def initField1(implicit session: Session) = // init code
val field1 = db withSession { implicit sesssion: Session => initField1 }
}
def initMyTrait(implicit session: Session) = new MyTrait {}
This is how my code currently looks - I can't find a good way to pass initMyTrait
's session to MyTrait
, and so I need to open a second session to initialize MyTrait.field1
. I could do something like
trait MyTrait {
private val _session: Session
private def initField1(implicit session: Session) = // init code
val field1 = initField1(_session)
}
def initMyTrait(implicit session: Session) = new MyTrait { private val _session = session }
but this will leave a closed session in MyTrait
after initialization
Upvotes: 0
Views: 95
Reputation: 11270
I would try to avoid needing a database connection for initializing the trait. An alternative could be a caching method which you use in place of the val, e.g.
private var field1Cache: ... = null
def field1(implicit session:Session) = {
if( field1Cache == null ){
field1Cache = initField1
}
field1Cache
}
Or you could make the trait a class and take a private argument.
Upvotes: 1