add connection pool to Database object
1 parent 135e1ef commit 172af307a66a8b6c48ed4432bd7fd1f7c87d7379
@Rodrigo Lazoti Rodrigo Lazoti authored on 20 Jan 2015
Showing 10 changed files
View
2
■■■
project/build.scala
"ch.qos.logback" % "logback-classic" % "1.0.13" % "runtime",
"org.eclipse.jetty" % "jetty-webapp" % "8.1.8.v20121106" % "container;provided",
"org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "container;provided;test" artifacts Artifact("javax.servlet", "jar", "jar"),
"junit" % "junit" % "4.11" % "test",
"com.mchange" % "c3p0" % "0.9.5",
"com.typesafe" % "config" % "1.2.1",
"com.typesafe.play" %% "twirl-compiler" % "1.0.2"
),
EclipseKeys.withSource := true,
javacOptions in compile ++= Seq("-target", "7", "-source", "7"),
View
7
src/main/resources/database.conf 0 → 100644
db {
driver = "org.h2.Driver"
url = "jdbc:h2:${DatabaseHome};MVCC=true"
user = "sa"
password = "sa"
}
View
30
src/main/scala/servlet/AutoUpdateListener.scala
import util.Directory._
import util.ControlUtil._
import util.JDBCUtil._
import org.eclipse.jgit.api.Git
import util.Directory
import util.{DatabaseConfig, Directory}
 
object AutoUpdate {
/**
System.setProperty("gitbucket.home", dataDir)
}
org.h2.Driver.load()
 
val context = event.getServletContext
context.setInitParameter("db.url", s"jdbc:h2:${DatabaseHome};MVCC=true")
 
defining(getConnection(event.getServletContext)){ conn =>
defining(getConnection()){ conn =>
logger.debug("Start schema update")
try {
defining(getCurrentVersion()){ currentVersion =>
if(currentVersion == headVersion){
 
def contextDestroyed(sce: ServletContextEvent): Unit = {
}
 
private def getConnection(servletContext: ServletContext): Connection =
private def getConnection(): Connection =
DriverManager.getConnection(
servletContext.getInitParameter("db.url"),
servletContext.getInitParameter("db.user"),
servletContext.getInitParameter("db.password"))
 
private def getDatabase(servletContext: ServletContext): scala.slick.jdbc.JdbcBackend.Database =
slick.jdbc.JdbcBackend.Database.forURL(
servletContext.getInitParameter("db.url"),
servletContext.getInitParameter("db.user"),
servletContext.getInitParameter("db.password"))
DatabaseConfig.url,
DatabaseConfig.user,
DatabaseConfig.password)
 
}
View
58
src/main/scala/servlet/TransactionFilter.scala
package servlet
 
import javax.servlet._
import javax.servlet.http.HttpServletRequest
import com.mchange.v2.c3p0.ComboPooledDataSource
import org.slf4j.LoggerFactory
import javax.servlet.http.HttpServletRequest
import util.Keys
import slick.jdbc.JdbcBackend.{Database => SlickDatabase, Session}
import util.{DatabaseConfig, Keys}
 
/**
* Controls the transaction with the open session in view pattern.
*/
class TransactionFilter extends Filter {
 
private val logger = LoggerFactory.getLogger(classOf[TransactionFilter])
 
def init(config: FilterConfig) = {}
 
def destroy(): Unit = {}
 
def doFilter(req: ServletRequest, res: ServletResponse, chain: FilterChain): Unit = {
if(req.asInstanceOf[HttpServletRequest].getRequestURI().startsWith("/assets/")){
// assets don't need transaction
chain.doFilter(req, res)
} else {
Database(req.getServletContext) withTransaction { session =>
Database() withTransaction { session =>
logger.debug("begin transaction")
req.setAttribute(Keys.Request.DBSession, session)
chain.doFilter(req, res)
logger.debug("end transaction")
}
 
object Database {
 
def apply(context: ServletContext): slick.jdbc.JdbcBackend.Database =
slick.jdbc.JdbcBackend.Database.forURL(context.getInitParameter("db.url"),
context.getInitParameter("db.user"),
context.getInitParameter("db.password"))
private val logger = LoggerFactory.getLogger(Database.getClass)
 
def getSession(req: ServletRequest): slick.jdbc.JdbcBackend#Session =
req.getAttribute(Keys.Request.DBSession).asInstanceOf[slick.jdbc.JdbcBackend#Session]
private val db: SlickDatabase = {
val datasource = new ComboPooledDataSource
 
datasource.setDriverClass(DatabaseConfig.driver)
datasource.setJdbcUrl(DatabaseConfig.url)
datasource.setUser(DatabaseConfig.user)
datasource.setPassword(DatabaseConfig.password)
 
logger.debug("load database connection pool")
 
SlickDatabase.forDataSource(datasource)
}
 
def apply(): SlickDatabase = db
 
def getSession(req: ServletRequest): Session =
req.getAttribute(Keys.Request.DBSession).asInstanceOf[Session]
 
}
View
3
■■
src/main/scala/ssh/GitCommand.scala
protected def runTask(user: String)(implicit session: Session): Unit
 
private def newTask(user: String): Runnable = new Runnable {
override def run(): Unit = {
Database(context) withSession { implicit session =>
Database() withSession { implicit session =>
try {
runTask(user)
callback.onExit(0)
} catch {
case _ => new UnknownCommand(command)
}
}
}
View
2
■■■
src/main/scala/ssh/PublicKeyAuthenticator.scala
 
class PublicKeyAuthenticator(context: ServletContext) extends PublickeyAuthenticator with SshKeyService {
 
override def authenticate(username: String, key: PublicKey, session: ServerSession): Boolean = {
Database(context) withSession { implicit session =>
Database() withSession { implicit session =>
getPublicKeys(username).exists { sshKey =>
SshUtil.str2PublicKey(sshKey.publicKey) match {
case Some(publicKey) => key.equals(publicKey)
case _ => false
View
20
src/main/scala/util/DatabaseConfig.scala 0 → 100644
package util
 
import com.typesafe.config.ConfigFactory
import util.Directory.DatabaseHome
 
object DatabaseConfig {
 
private val config = ConfigFactory.load("database")
private val dbUrl = config.getString("db.url")
 
def url(directory: Option[String]): String =
dbUrl.replace("${DatabaseHome}", directory.getOrElse(DatabaseHome))
 
val url: String = url(None)
val user: String = config.getString("db.user")
val password: String = config.getString("db.password")
val driver: String = config.getString("db.driver")
 
}
View
src/main/scala/util/Notifier.scala
View
src/main/webapp/WEB-INF/web.xml
View
src/test/scala/service/ServiceSpecBase.scala