Multiple DBs

19 views
Skip to first unread message

MrWHO

unread,
Jul 15, 2009, 5:41:21 AM7/15/09
to Lift

I can bind a Lift application to MySQL or any other DB quite easily.
That's fine and dandy until I need to pull information from different
DBs. Is there a way to bind different models to different backend DBs?

Say that I want to keep application related objects (users, groups,
preferences, etc.) in a different DB compared to the one where I have
the actual data users will be accessing, how would I go about it?

cheers,

Fabio

Derek Chen-Becker

unread,
Jul 15, 2009, 3:51:21 PM7/15/09
to lif...@googlegroups.com
You can use different net.liftweb.mapper.ConnectionIdentifiers to represent different DBs. Then there are MetaMapper methods that take ConnectionIdentifiers to tell it which connection to use. For example, in Boot you can do:

object FirstDB extends ConnectionIdentifier { def jndiName = "First" }
object SecondDB extends ConnectionIdentifier {def jndiName = "Second" }

object MyConnectionManager extends ConnectionManager {
  def newConnection (name : ConnectionIdentifier) = name match {
    case FirstDB => // open a connection for your first DB
    case SecondDB => // open a connection for the second DB
  }
  ...
}

def boot {
  DB.defineConnectionManager(FirstDB, MyConnectionManager)
  DB.defineConnectionManager(SecondDB, MyConnectionManager)
  ...
}

Then in your code you can do things like:

User.findAllDb(FirstDB)

or, more useful, you can set the connection identifier on a per-entity basis using dbDefaultConnectionIdentifier on the MetaMapper:

object Widget extends Widget with MetaMapper[Widget] {
  override def dbDefaultConnectionIdentifier = FirstDB
}

object Gizmo extends Gizmo with MetaMapper[Gizmo] {
  override def dbDefaultConnectionIdentifier = SecondDB
}

Derek

MrWHO

unread,
Jul 22, 2009, 4:34:08 AM7/22/09
to Lift

Thank you! Exactly what I was looking for... tested and tried :)

Fabio
Reply all
Reply to author
Forward
0 new messages