mirror of
				https://github.com/gitbucket/gitbucket.git
				synced 2025-11-03 20:15:59 +01:00 
			
		
		
		
	Fix db connection error in dev mode
Changes to close the no longer needed DB connections.
This commit is contained in:
		@@ -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()
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user