即使autocommit为false,MyBatis也会发出声明

时间:2014-10-27 05:53:08

标签: java jdbc mybatis

我一直在使用带有C3p0的myBatis 3.2.8作为数据源。最近我遇到了一些奇怪的问题。 基本上,myBatis提交我的INSERT语句,即使自动提交显式传递为FALSE且session.commit()甚至没有被调用。 这是我的代码块的样子:

SqlSession session = factory.openSession(false);
try {
  mapper.insert(myObject);
  doStuff();
  session.commit();
}
catch (Throwable t)
{
  session.rollback();
  // log exception
}
finally {
  session.close();
}

基本上我观察到的是,当调用mapper.insert(myObject)时,数据立即存储到我的数据库中,因为我在调试器中单步执行此语句,我看到了这些日志:

DEBUG [2014-10-27 05:30:34,730] com.mchange.v2.resourcepool.BasicResourcePool: decremented pending_acquires: 0
DEBUG [2014-10-27 05:30:34,730] org.apache.ibatis.transaction.jdbc.JdbcTransaction: Setting autocommit to false on JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@33dbc40]
DEBUG [2014-10-27 05:30:34,773] com.liangzhi.core.api.database.mapper.ProjectMapper.insertProject: ==>  Preparing: INSERT INTO projects (ProjectName, ProjectAbstractText, ProjectFairID) VALUES (?, ?, ?); 
DEBUG [2014-10-27 05:30:34,975] com.liangzhi.core.api.database.mapper.ProjectMapper.insertProject: ==> Parameters: 3568bca0-2bc2-4610-b6f1-a6858752817c(String), 3568bca0-2bc2-4610-b6f1-a6858752817c(String), 1(Integer)
DEBUG [2014-10-27 05:30:35,018] com.liangzhi.core.api.database.mapper.ProjectMapper.insertProject: <==    Updates: 1
DEBUG [2014-10-27 05:30:35,018] org.apache.ibatis.transaction.jdbc.JdbcTransaction: Committing JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@33dbc40]
DEBUG [2014-10-27 05:30:35,168] org.apache.ibatis.transaction.jdbc.JdbcTransaction: Resetting autocommit to true on JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@33dbc40]
DEBUG [2014-10-27 05:30:35,170] org.apache.ibatis.transaction.jdbc.JdbcTransaction: Closing JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@33dbc40]

我感到很困惑,因为我还没有调用关闭连接,而且它已经关闭了JDBC连接。 请注意&#34; false&#34;为factory.openSession()传递以禁用autoCommit。

这是我的myBatis配置:

<environments default="dev">
        <environment id="dev">
            <transactionManager type="JDBC">
                <property name="JDBC.autoCommit" value="false" />  
            </transactionManager>
            <dataSource type="com.liangzhi.core.api.database.C3P0DataSourceFactory">
                <property name="driverClass" value="${dev.driver}" />
                <property name="jdbcUrl" value="${dev.url}" />
                <property name="user" value="${dev.username}" />
                <property name="password" value="${dev.password}" />
                <property name="idleConnectionTestPeriod" value="60"/>
                <property name="maxPoolSize" value="20"/>
                <property name="maxIdleTime" value="600"/>
                <property name="preferredTestQuery" value="SELECT 1"/>  
                <property name="autoCommitOnClose" value="false"/>
            </dataSource>
        </environment>
    </environments>

这是我的数据源类

public class C3P0DataSourceFactory extends UnpooledDataSourceFactory {

  public C3P0DataSourceFactory() {
    this.dataSource = new ComboPooledDataSource();
  }
}

请帮帮我!非常感谢!

0 个答案:

没有答案