1

我找不到如何在 Grails 中激活连接池。

在我的 DataSource.groovy 中,我有以下内容:

        url = "jdbc:mysql://localhost/myapp?useUnicode=yes&characterEncoding=UTF-8"
        username = "root"
        password = ""
        dialect = org.hibernate.dialect.MySQL5InnoDBDialect
        pooled = true
        properties {
            maxActive = -1
            minEvictableIdleTimeMillis=1800000
            timeBetweenEvictionRunsMillis=1800000
            numTestsPerEvictionRun=3
            testOnBorrow=true
            testWhileIdle=true
            testOnReturn=true
            validationQuery = "select 1"

        }

除了设置 pooled = true 之外,我还需要做些什么来激活连接池吗?

4

2 回答 2

2

不,该pooled属性配置池。如果设置为,false那么您每次都会创建一个新连接,这可能需要数百毫秒,因此不建议这样做。

但是,您可以pooled = false在使用 JNDI 时进行设置,因为 JNDI 数据源自己进行池化,因此无需两次池化。

于 2013-09-01T19:30:35.163 回答
0

下面是来自 Grails DataSource文档页面的示例:

dataSource {
pooled = true
dbCreate = "update"
url = "jdbc:mysql://localhost:3306/my_database"
driverClassName = "com.mysql.jdbc.Driver"
dialect = org.hibernate.dialect.MySQL5InnoDBDialect
username = "username"
password = "password"
properties {
   jmxEnabled = true
   initialSize = 5
   maxActive = 50
   minIdle = 5
   maxIdle = 25
   maxWait = 10000
   maxAge = 10 * 60000
   timeBetweenEvictionRunsMillis = 5000
   minEvictableIdleTimeMillis = 60000
   validationQuery = "SELECT 1"
   validationQueryTimeout = 3
   validationInterval = 15000
   testOnBorrow = true
   testWhileIdle = true
   testOnReturn = false
   jdbcInterceptors = "ConnectionState;StatementCache(max=200)"
   defaultTransactionIsolation = java.sql.Connection.TRANSACTION_READ_COMMITTED
}}

除了将 pooled 设置为 true 之外,您还可以调整预先初始化的连接数 (initialSize) 以及当负载变高时您希望保持的最大连接数 (maxActive)。

于 2014-08-05T09:15:33.063 回答