我遇到了一些奇怪的问题。
我使用池来创建和管理数据库连接,我将DefaultAutocommit
选项设置为FALSE
.
但过了一会儿,当发生错误并rollback
调用 a 时,会抛出异常:Can't call rollback when autocommit=true
重新启动 JBoss 将解决问题,因为将创建一个新的 DataSource。
这是我创建数据源的方式:
protected DataSource getDataSource(String driverClassName, String dbUrl, String dbUser, String dbPwd) {
PoolProperties poolProperties = new PoolProperties();
poolProperties.setUrl(dbUrl);
poolProperties.setDriverClassName(driverClassName);
poolProperties.setUsername(dbUser);
poolProperties.setPassword(dbPwd);
poolProperties.setDefaultAutoCommit(false);
poolProperties.setTestWhileIdle(false);
poolProperties.setTestOnBorrow(true);
poolProperties.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
poolProperties.setValidationQuery("SELECT 1");
poolProperties.setTestOnReturn(false);
poolProperties.setLogAbandoned(false);
poolProperties.setRemoveAbandoned(true);
poolProperties.setRemoveAbandonedTimeout(20);
poolProperties.setMaxActive(100);
poolProperties.setInitialSize(10);
poolProperties.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
return new DataSource(poolProperties);
}
以及我如何获得连接:
xxx.getDataSource().getConnection();
我还没有尝试,但我的第一个电话是使用setAutoCommit(false)
.
虽然我不明白为什么poolProperties.setDefaultAutoCommit(false);
停止工作。
堆栈跟踪 :
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Can't call rollback when autocommit=true
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
at com.mysql.jdbc.ConnectionImpl.rollback(ConnectionImpl.java:4805)
at sun.reflect.GeneratedMethodAccessor302.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:125)
at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:94)
at org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor.invoke(AbstractCreateStatementInterceptor.java:71)
at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:94)
at org.apache.tomcat.jdbc.pool.interceptor.ConnectionState.invoke(ConnectionState.java:140)
at $Proxy333.rollback(Unknown Source)