tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Cheshire <yahoono...@gmail.com>
Subject tomcat 8.5.23 dbcp not honoring autocommit = false?
Date Wed, 11 Oct 2017 21:21:47 GMT
Working on a migration from 7 to 8.5, and in it I am now using the
tomcat dbcp, instead of apache commons dbcp. I have found that with no
other changes to the db code (except the factory param for the
resource), it is working fine other than there is an implicit commit
happening when I close a connection, even with autocommit turned off
in mysql config, resource config AND in my code.

Resource config :

<Resource name="jdbc/mysql"
          auth="Container"
          type="javax.sql.DataSource"
          driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://localhost:3306/mydb?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false"
          factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
          username=""
          password=""
          maxActive="150"
          maxIdle="25"
          maxWait="60000"
          removeAbandoned="true"
          removeAbandonedTimeout="300"
          logAbandoned="true"
          validationQuery="/* ping */"
          testOnBorrow="true"
          testWhileIdle="true"
          timeBetweenEvictionRunsMillis="600000"
          defaultAutoCommit="false" />

Only thing changed in that from 7.x to 8.5.x is the factory was
org.apache.commons.dbcp.BasicDataSourceFactory. I am using Connector/J
5.1.44 (latest version).


Getting a connection boils down to this in my code (pieces pulled out
of factories and other classes)

(Support class in web code)
public static DataSource getDataSource() {
    try {
        return (DataSource)new
InitialContext().lookup("java:comp/env/" +
ServletContextParameters.getDatabaseResourceName());
    }
    catch (NamingException ex) {
        throw new RuntimeException("unable to find datasource", ex);
    }
}


(DAO Factory implementation)
public MySQLDAOFactoryImpl(@NotNull DataSource dataSource) {
    this.dataSource = dataSource;

    try {
        this.dbConn = this.dataSource.getConnection();
        this.dbConn.setAutoCommit(false);
        this.dbConn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
    }
    catch (SQLException ex) {
        throw new DAOException("unable to get database connection", ex);
    }
}

@Override
public void close() {
    try {
        if (this.dbConn != null) {
            this.dbConn.close();
        }
    }
    catch (SQLException ex) {
        throw new DAOException("error closing database connection", ex);
    }
}


If I do

daoFactory = new MySQLDAOFactoryImpl(getDataSource());

// update #1
daoFactory.commit()

// update #2
daoFactory.close();

then update #2 is being committed.

If I put in this in the close() method of my DAO Factory

if (!this.dbConn.getAutoCommit()) {
    this.dbConn.rollback();
}

before the close() call, then update #2 is correctly not getting committed.

I looked back through the recent tomcat changelogs, and found a
reference to https://bz.apache.org/bugzilla/show_bug.cgi?id=61425
under the 8.5.21 release which looks like it might be addressing this
problem. However, when I download the source for 8.5.23, there is no
org.apache.tomcat.jdbc directory so I can't dive in there.

Anyone else experienced this? Hopefully I am just missing something obvious.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message