Created
August 12, 2009 19:16
-
-
Save eltimn/166687 to your computer and use it in GitHub Desktop.
H2 connection manager for Lift app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
H2 has it's own connection pool manager that H2 claims [1] is faster than using | |
DriverManager.getConnection(). Here's how to configure it for use in a Lift app. | |
/** | |
* H2 db connection manager | |
*/ | |
object H2DBVendor extends ConnectionManager { | |
// create the connection pool | |
private val cp = org.h2.jdbcx.JdbcConnectionPool.create( | |
Props.get("db.url") openOr "jdbc:h2:data/test", | |
Props.get("db.user") openOr "", | |
Props.get("db.password") openOr "" | |
) | |
// set max connections | |
cp.setMaxConnections(10) // default = 10 | |
// set max time to wait for an available connection | |
cp.setLoginTimeout(60) // default = 60 seconds | |
// get a new connection | |
def newConnection(name: ConnectionIdentifier): Box[Connection] = { | |
try { | |
Full(cp.getConnection) | |
} | |
catch { | |
case e: Exception => e.printStackTrace; Empty | |
} | |
} | |
def releaseConnection(conn: Connection) { | |
conn.close // this puts the connection back into the pool | |
} | |
def shutdown() { | |
cp.dispose // dispose of the connection pool | |
} | |
} | |
Then in Boot.boot, add: | |
DB.defineConnectionManager(DefaultConnectionIdentifier, H2DBVendor) | |
LiftRules.unloadHooks.append(H2DBVendor.shutdown) // optional | |
The last line will close the connection pool when the servlet context is | |
destroyed. Helpful if you need the db shutdown when the JVM is still running. | |
[1] http://h2database.com/html/tutorial.html#connection_pool |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment