Skip to content

Instantly share code, notes, and snippets.

@jaytaylor
Created March 22, 2012 23:09
Show Gist options
  • Save jaytaylor/2165347 to your computer and use it in GitHub Desktop.
Save jaytaylor/2165347 to your computer and use it in GitHub Desktop.
Play Framework 1.x C3PO Database pool configurator
import play.{configuration, Logger}
import com.mchange.v2.c3p0.ComboPooledDataSource
/**
* @author Jay Taylor [@jtaylor]
*
* @date 2012-03-22
*
* @description c3po Database connection pool configuration container and applicator.
*
* Example configuration details to add to 'conf/application.conf':
*
* # Database Pool Configuration
* # ~~~~~
* #
* db.pool.driverClass=com.mysql.jdbc.Driver
* db.pool.url=jdbc:mysql://localhost/myApp?sessionVariables=storage_engine=InnoDB,character_set_server=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true
* %stage.db.pool.url=jdbc:mysql://staging/myApp?sessionVariables=storage_engine=InnoDB,character_set_server=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true
* %prod.db.pool.url=jdbc:mysql://localhost/myApp?sessionVariables=storage_engine=InnoDB,character_set_server=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true
* db.pool.username=root
* db.pool.password=
* %stage.db.pool.username=root
* %stage.db.pool.password=
*
* # NB: MySQL configuration for testing.
* # It's unfortunate that as of Play 1.2.3, whenever you run "play test" or
* # "play auto-test", you are automatically running with environment id "test".
* # That's a shame, because it means that any workspace in which you want to run·
* # tests has to access the database (and anything else) with the same
* # configuration. I.e. hostnames, userids, etc. For the moment, we'll live with
* # it.·
* #·
* %test.db.pool.url=jdbc:mysql://staging/myAppTest?sessionVariables=storage_engine=InnoDB,character_set_server=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true
*
* # c3po Database Connection Pooling configuration
* # See http://www.mchange.com/projects/c3p0/index.html for more details.
* db.pool.initialSize=2
* db.pool.minSize=2
* db.pool.maxSize=10
* db.pool.acquireIncrement=2
* %prod.db.pool.initialSize=25
* %prod.db.pool.minSize=10
* %prod.db.pool.maxSize=100
*
* # in seconds
* db.pool.idleConnectionTestPeriod=30
* db.pool.maxIdleTime=45
* db.pool.maxIdleTimeExcessConnections=60
* db.pool.maxConnectionAge=55
*
* # Retry forever
* db.pool.acquireRetryAttempts=0
*
* db.pool.testTableName=c3poTestTable
* db.pool.preferredTestQuery=SELECT 1
* db.pool.testConnectionOnCheckout=true
* db.pool.testConnectionOnCheckin=true
*
* Example class usage:
*
* val pool = DatabasePoolConfigurator("db.pool")(new ComboPooledDataSource)
*/
case class DatabasePoolConfigurator(prefix: String) {
val driverClass: String = configuration(prefix + ".driverClass", "com.mysql.jdbc.Driver")
val username: String = configuration(prefix + ".username", "root")
val password: String = configuration(prefix + ".password", "")
val jdbcUrl: String = configuration(prefix + ".url", "jdbc:mysql://localhost/myApp?sessionVariables=storage_engine=InnoDB,character_set_server=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true")
val initialPoolSize: Int = configuration(prefix + ".initialSize", "25").toInt
val minPoolSize: Int = configuration(prefix + ".minSize", "25").toInt
val maxPoolSize: Int = configuration(prefix + ".maxSize", "100").toInt
val acquireIncrement: Int = configuration(prefix + ".acquireIncrement", "3").toInt
val idleConnectionTestPeriod: Int = configuration(prefix + ".idleConnectionTestPeriod", "30").toInt
val maxIdleTime: Int = configuration(prefix + ".maxIdleTime", "0").toInt
val maxIdleTimeExcessConnections: Int = configuration(prefix + ".maxIdleTimeExcessConnections", "0").toInt
val maxConnectionAge: Int = configuration(prefix + ".maxConnectionAge", "0").toInt
val acquireRetryAttempts: Int = configuration(prefix + ".acquireRetryAttempts", "30").toInt
val automaticTestTable: String = configuration(prefix + ".testTableName", "c3poTestTable")
val preferredTestQuery: String = configuration(prefix + ".preferredTestQuery", "SELECT 1")
val testConnectionOnCheckin: Boolean = configuration(prefix + ".testConnectionOnCheckin", "false").toBoolean
val testConnectionOnCheckout: Boolean = configuration(prefix + ".testConnectionOnCheckout", "true").toBoolean
/**
* Applies this c3po configuration to a pooled data source.
*/
def apply(pool: ComboPooledDataSource): ComboPooledDataSource = {
Logger info "Applying the following configuration:\n" + this.toString
pool setDriverClass driverClass
pool setUser username
pool setPassword password
pool setJdbcUrl jdbcUrl
pool setInitialPoolSize initialPoolSize
pool setMinPoolSize minPoolSize
pool setMaxPoolSize maxPoolSize
pool setAcquireIncrement acquireIncrement
pool setIdleConnectionTestPeriod idleConnectionTestPeriod
pool setMaxIdleTime maxIdleTime
pool setMaxIdleTimeExcessConnections maxIdleTimeExcessConnections
pool setMaxConnectionAge maxConnectionAge
pool setAcquireRetryAttempts acquireRetryAttempts
pool setAutomaticTestTable automaticTestTable
pool setPreferredTestQuery preferredTestQuery
pool setTestConnectionOnCheckin testConnectionOnCheckin
pool setTestConnectionOnCheckout testConnectionOnCheckout
pool
}
override val toString: String =
"""DatabasePoolConfiguration(prefix = "%s") ⇒ {
driverClass : """ + driverClass + """
username : """ + username + """
password : """ + password + """
jdbcUrl : """ + jdbcUrl + """
initialPoolSize : """ + initialPoolSize + """
minPoolSize : """ + minPoolSize + """
maxPoolSize : """ + maxPoolSize + """
acquireIncrement : """ + acquireIncrement + """
idleConnectionTestPeriod : """ + idleConnectionTestPeriod + """
maxIdleTime : """ + maxIdleTime + """
maxIdleTimeExcessConnections : """ + maxIdleTimeExcessConnections + """
maxConnectionAge : """ + maxConnectionAge + """
acquireRetryAttempts : """ + acquireRetryAttempts + """
automaticTestTable : """ + automaticTestTable + """
preferredTestQuery : """ + preferredTestQuery + """
testConnectionOnCheckin : """ + testConnectionOnCheckin + """
testConnectionOnCheckout : """ + testConnectionOnCheckout + "\n}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment