Fix db connection error in dev mode

Changes to close the no longer needed DB connections.
This commit is contained in:
jparound30
2015-04-11 13:32:29 +09:00
parent 2f00060c57
commit 74e18a982d
3 changed files with 21 additions and 14 deletions

View File

@@ -1,7 +1,7 @@
import gitbucket.core.controller._
import gitbucket.core.plugin.PluginRegistry
import gitbucket.core.servlet.{AccessTokenAuthenticationFilter, BasicAuthenticationFilter, TransactionFilter}
import gitbucket.core.servlet.{AccessTokenAuthenticationFilter, BasicAuthenticationFilter, Database, TransactionFilter}
import gitbucket.core.util.Directory
import java.util.EnumSet
@@ -47,4 +47,8 @@ class ScalatraBootstrap extends LifeCycle {
dir.mkdirs()
}
}
override def destroy(context: ServletContext): Unit = {
Database.closeDataSource()
}
}

View File

@@ -14,7 +14,6 @@ import JDBCUtil._
import org.eclipse.jgit.api.Git
import gitbucket.core.util.Versions
import gitbucket.core.util.Directory
import gitbucket.core.plugin._
object AutoUpdate {
@@ -182,7 +181,7 @@ class InitializeListener extends ServletContextListener with SystemSettingsServi
}
org.h2.Driver.load()
defining(getConnection()){ conn =>
using(getConnection()){ conn =>
// Migration
logger.debug("Start schema update")
Versions.update(conn, headVersion, getCurrentVersion(), versions, Thread.currentThread.getContextClassLoader){ conn =>
@@ -195,9 +194,11 @@ class InitializeListener extends ServletContextListener with SystemSettingsServi
}
def contextDestroyed(event: ServletContextEvent): Unit = {
override def contextDestroyed(event: ServletContextEvent): Unit = {
// Shutdown plugins
PluginRegistry.shutdown(event.getServletContext, loadSystemSettings())
// Close datasource
Database.closeDataSource()
}
private def getConnection(): Connection =
@@ -205,5 +206,4 @@ class InitializeListener extends ServletContextListener with SystemSettingsServi
DatabaseConfig.url,
DatabaseConfig.user,
DatabaseConfig.password)
}

View File

@@ -39,17 +39,18 @@ object Database {
private val logger = LoggerFactory.getLogger(Database.getClass)
private val db: SlickDatabase = {
val datasource = new ComboPooledDataSource
datasource.setDriverClass(DatabaseConfig.driver)
datasource.setJdbcUrl(DatabaseConfig.url)
datasource.setUser(DatabaseConfig.user)
datasource.setPassword(DatabaseConfig.password)
private val dataSource: ComboPooledDataSource = {
val ds = new ComboPooledDataSource
ds.setDriverClass(DatabaseConfig.driver)
ds.setJdbcUrl(DatabaseConfig.url)
ds.setUser(DatabaseConfig.user)
ds.setPassword(DatabaseConfig.password)
logger.debug("load database connection pool")
ds
}
SlickDatabase.forDataSource(datasource)
private val db: SlickDatabase = {
SlickDatabase.forDataSource(dataSource)
}
def apply(): SlickDatabase = db
@@ -57,4 +58,6 @@ object Database {
def getSession(req: ServletRequest): Session =
req.getAttribute(Keys.Request.DBSession).asInstanceOf[Session]
def closeDataSource(): Unit = dataSource.close
}