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


On Sat, Dec 5, 2009 at 9:47 PM, Neil.Lv <[email protected]> wrote:

> Hi all,
>
>  I want to use two databases, but i don't know how to configure it.
>
>  Does anybody know that how to configure two database connection in
> Lift?
>
>
> 1:
>  I add two ConnectionIdentifier in the Boot.class
> ###
> object OneDB extends ConnectionIdentifier {
>
>  def jndiName = "one"
>
> }
>
> object TwoDB extends ConnectionIdentifier {
>
>  def jndiName = "two"
>
> }
> ###
>
> 2: In the User model
>
>   How can i write the code in the method,
> ###
>  override def dbCalculateConnectionIdentifier = {
>    Two
>  }
> ###
>
>  Thanks for any suggestion!
>
> Cheers,
>  Neil
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "Lift" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<liftweb%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/liftweb?hl=en.
>
>
>


-- 
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics

--

You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en.


Attachment: twofer.tgz
Description: GNU Zip compressed data

Reply via email to