I've put together some sample code that describes how to access multiple databases from a single Lift app. It's enclosed.
First, you need to identify different ConnectionIdentifiers for each connection:
package com.liftcode.model
import net.liftweb._
import mapper._
case object CatConnectionIdentifier extends ConnectionIdentifier {
def jndiName: String = "cat"
}
case object DogConnectionIdentifier extends ConnectionIdentifier {
def jndiName: String = "dog"
}
Tell your Mapper classes what their default DB is:
package com.liftcode.model
import net.liftweb._
import mapper._
class Cat extends LongKeyedMapper[Cat] with IdPK {
def getSingleton = Cat
object name extends MappedString(this, 64)
}
object Cat extends Cat with LongKeyedMetaMapper[Cat] {
override def dbDefaultConnectionIdentifier = CatConnectionIdentifier
}
class Dog extends LongKeyedMapper[Dog] with IdPK {
def getSingleton = Dog
object name extends MappedString(this, 64)
}
object Dog extends Dog with LongKeyedMetaMapper[Dog] {
override def dbDefaultConnectionIdentifier = DogConnectionIdentifier
}
You need to hook the ConnectionIdentifiers up to the actual databases... in Boot:
DB.defineConnectionManager(DefaultConnectionIdentifier,
new StandardDBVendor(Props.get("db.driver") openOr "org.h2.Driver",
Props.get("db.url") openOr "jdbc:h2:lift_proto.db",
Props.get("db.user"), Props.get("db.password")))
DB.defineConnectionManager(CatConnectionIdentifier,
new StandardDBVendor(Props.get("db.driver") openOr "org.h2.Driver",
Props.get("db.url") openOr "jdbc:h2:lift_cat.db",
Props.get("db.user"), Props.get("db.password")))
DB.defineConnectionManager(DogConnectionIdentifier,
new StandardDBVendor(Props.get("db.driver") openOr "org.h2.Driver",
Props.get("db.url") openOr "jdbc:h2:lift_dog.db",
Props.get("db.user"), Props.get("db.password")))
And Schemify based on the correct identifier:
Schemifier.schemify(true, Log.infoF _, DefaultConnectionIdentifier, User)
Schemifier.schemify(true, Log.infoF _, CatConnectionIdentifier, Cat)
Schemifier.schemify(true, Log.infoF _, DogConnectionIdentifier, Dog)
And finally make sure that the transactions are wrapped correctly:
S.addAround(DB.buildLoanWrapper(List(DefaultConnectionIdentifier, DogConnectionIdentifier, CatConnectionIdentifier)))
Cats will come from the Cat DB, dogs from the Dog DB.
It's possible to get shardy with this setup as well, but this should suffice.
Thanks,
David
--
Lift, the simply functional web framework
http://liftweb.netBeginning Scala
http://www.apress.com/book/view/1430219890
Follow me:
http://twitter.com/dppSurf the harmonics