Browse Source

Optimize MySQL JDBC connection properties

Lari Hotari 11 years ago
parent
commit
adf007eb0b

+ 36 - 17
grails/hello/grails-app/conf/DataSource.groovy

@@ -1,31 +1,50 @@
+import org.springframework.beans.factory.config.PropertiesFactoryBean
+import org.springframework.core.io.support.ResourceArrayPropertyEditor
+
 dataSource {
     pooled = true
     dbCreate = "update"
-    url = "jdbc:mysql://localhost:3306/hello_world?jdbcCompliantTruncation=false&elideSetAutoCommits=true&useLocalSessionState=true&cachePrepStmts=true&cacheCallableStmts=true&alwaysSendSetIsolation=false&prepStmtCacheSize=4096&cacheServerConfiguration=true&prepStmtCacheSqlLimit=2048&zeroDateTimeBehavior=convertToNull&traceProtocol=false&useUnbufferedInput=false&useReadAheadInput=false&maintainTimeStats=false&useServerPrepStmts&cacheRSMetadata=true"
+    url = "jdbc:mysql://localhost:3306/hello_world"
     driverClassName = "com.mysql.jdbc.Driver"
     dialect = org.hibernate.dialect.MySQL5InnoDBDialect
     username = "benchmarkdbuser"
     password = "benchmarkdbpass"
-
-	properties {
-       maxActive = 256
-       maxIdle = 25
-       minIdle = 5
-       initialSize = 5
-       minEvictableIdleTimeMillis = 60000
-       timeBetweenEvictionRunsMillis = 60000
-       maxWait = 10000
-       numTestsPerEvictionRun=3
-       testOnBorrow=false
-       testWhileIdle=true
-       testOnReturn=false
-       validationQuery="/* ping */"
-       jdbcInterceptors="ConnectionState"
-	}
+    properties {
+        fairQueue = false
+        maxActive = 256
+        maxIdle = 25
+        minIdle = 5
+        initialSize = 5
+        minEvictableIdleTimeMillis = 60000
+        timeBetweenEvictionRunsMillis = 60000
+        maxWait = 10000
+        maxAge = 1800 * 1000
+        numTestsPerEvictionRun=3
+        testOnBorrow=false
+        testWhileIdle=true
+        testOnReturn=false
+        validationQuery="/* ping */"
+        validationInterval=15000
+        jdbcInterceptors="ConnectionState"
+        defaultTransactionIsolation = java.sql.Connection.TRANSACTION_READ_UNCOMMITTED
+        dbProperties = this.loadProperties("classpath:mysql-connection.properties")
+    }
 }
+
 hibernate {
     // Purposely turning off query cache
     cache.use_second_level_cache = false
     cache.use_query_cache = false
     cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
 }
+
+static Properties loadProperties(String path) {
+    PropertiesFactoryBean pfb=new PropertiesFactoryBean()
+    pfb.setIgnoreResourceNotFound(true)
+    def converter=new ResourceArrayPropertyEditor()
+    converter.setAsText(path)
+    pfb.setLocations(converter.getValue())
+    pfb.afterPropertiesSet()
+    return pfb.object
+}
+

+ 56 - 0
grails/hello/grails-app/conf/mysql-connection.properties

@@ -0,0 +1,56 @@
+# MYSQL JDBC Connection properties
+# http://dev.mysql.com/doc/refman/5.5/en/connector-j-reference-configuration-properties.html
+jdbcCompliantTruncation=false
+autoReconnect=false
+zeroDateTimeBehavior=convertToNull
+
+# disable unicode handling
+useUnicode=false
+
+# charset
+#useUnicode=true
+#characterEncoding=Cp1252
+
+#dumpQueriesOnException=true
+dontTrackOpenResources=true
+
+#useCompression=true
+#tcpRcvBuf=262144
+#tcpSndBuf=262144
+
+# setAutocommit / setIsolationLevel optimizations
+useLocalSessionState=true
+useLocalTransactionState=true
+elideSetAutoCommits=true
+alwaysSendSetIsolation=false
+relaxAutoCommit=true
+
+# metadata caching
+cacheServerConfiguration=true
+cacheResultSetMetadata=true
+metadataCacheSize=100
+
+# enable MySQL query cache - server prep stmts wont use caching
+useServerPrepStmts=false
+
+# prepared statement caching on JDBC client
+cachePrepStmts=true
+cacheCallableStmts=true
+prepStmtCacheSize=4096
+prepStmtCacheSqlLimit=32000
+
+# log slow queries
+#logSlowQueries=true
+#slowQueryThresholdMillis=2000
+
+noDatetimeStringSync=true
+enableQueryTimeouts=false
+
+traceProtocol=false
+useUnbufferedInput=true
+useReadAheadInput=true
+maintainTimeStats=false
+
+# timeouts for TCP/IP
+connectTimeout=15000
+socketTimeout=120000