JNDI、mysql 5.6 和 tomcat 7 的大多数示例都在 DataSource.groovy 中定义了这一点:

dataSource {
   pooled = true
   driverClassName = "com.mysql.jdbc.Driver"
   dialect = 'org.hibernate.dialect.MySQL5InnoDBDialect'
}
environments:
   production {
        dataSource {
        dbCreate = "update"
        jndiName = "java:comp/env/myDatasourceName
    }
}

并在 tomcat 的 conf/context.xml 中:
<Context>
   <WatchedResource>WEB-INF/web.xml</WatchedResource>
      <Resource name="myDatasourceName" auth="Container" type="javax.sql.DataSource"
       maxActive="100" maxIdle="30" maxWait="10000"
       username="root" password="password" driverClassName="com.mysql.jdbc.Driver"
       url="jdbc:mysql://localhost:3306/my_db_name"/>
 </Context>

问题是,对于基本的小批量生产系统来说,这是正确的设置吗,如果数据库出现故障然后又出现等情况,它应该重新连接吗?

在 DataSource.xml 中有一个非常复杂的例子:
    dataSource {
        dbCreate = "update"
        //url = "jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10099;DB_CLOSE_ON_EXIT=FALSE"
        properties {
           // Documentation for Tomcat JDBC Pool
           // http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html#Common_Attributes
           // https://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/tomcat/jdbc/pool/PoolConfiguration.html
           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
           ignoreExceptionOnPreLoad = true
           // http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html#JDBC_interceptors
           jdbcInterceptors = "ConnectionState;StatementCache(max=200)"
           defaultTransactionIsolation = java.sql.Connection.TRANSACTION_READ_COMMITTED // safe default
           // controls for leaked connections
           abandonWhenPercentageFull = 100 // settings are active only when pool is full
           removeAbandonedTimeout = 120000
           removeAbandoned = true
           // use JMX console to change this setting at runtime
           logAbandoned = false // causes stacktrace recording overhead, use only for debugging
           /*
           // JDBC driver properties
           // Mysql as example
           dbProperties {
               // Mysql specific driver properties
               // http://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html
               // let Tomcat JDBC Pool handle reconnecting
               autoReconnect=false
               // truncation behaviour
               jdbcCompliantTruncation=false
               // mysql 0-date conversion
               zeroDateTimeBehavior='convertToNull'
               // Tomcat JDBC Pool's StatementCache is used instead, so disable mysql driver's cache
               cachePrepStmts=false
               cacheCallableStmts=false
               // Tomcat JDBC Pool's StatementFinalizer keeps track
               dontTrackOpenResources=true
               // performance optimization: reduce number of SQLExceptions thrown in mysql driver code
               holdResultsOpenOverStatementClose=true
               // enable MySQL query cache - using server prep stmts will disable query caching
               useServerPrepStmts=false
               // metadata caching
               cacheServerConfiguration=true
               cacheResultSetMetadata=true
               metadataCacheSize=100
               // timeouts for TCP/IP
               connectTimeout=15000
               socketTimeout=120000
               // timer tuning (disable)
               maintainTimeStats=false
               enableQueryTimeouts=false
               // misc tuning
               noDatetimeStringSync=true
           }
           */
        }
    }

现在我不知道其中 95% 的参数是做什么的,但我想我需要它们全部?

我不明白的是这个
  • 肯定 tomcat 会汇集连接,所以应该在 grails 数据源中 pooled=true 吗?
  • 肯定tomcat会处理连接的重连和验证,那么为什么要定义validationQuery = "SELECT 1"呢?特别是在 mysql dbPrperties 部分中说“autoREconnect=false”。
  • 有人能想出一个适用于少数用户的mysql的通用默认数据源定义吗?我可以调整最小/最大事件/空闲等,但这不是在 tomcat 中完成的,不是在 grails 中完成的吗?
  • "userServerPrepStmts=false"为什么有人要禁用准备好的语句?在过去的 15 年里,我们一直在训练只使用准备好的语句,否则数据库无法缓存它们。
  • 如果我忽略所有这个复杂的例子,并使用每个人都发布的 super 简单的方法,它会正常工作吗?例如当数据库下降和上升时它会重新连接吗?会不会漏水等

  • 谢谢!

    最佳答案

    AFAIK,Tomcat 与管理数据库连接无关。由于 Grails 2.3.6 ,这些是推荐的默认数据源属性:

    properties {
      //see http://grails.org/doc/latest/guide/conf.html#dataSource for documentation
       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
    }
    

    10-06 14:37
    查看更多