关于Ibatis,spring ,proxool整合后,无法设置事务不自动提交问题。
我在用ibatis的 executeBatch 做批量增加时,发现dao中批量增加方法无效,还是每一条语句一个事务执行的,我设置了事务为false也不行,不起作用,是不是spring配置有问题呢,麻烦老大帮看下,应该怎么弄呢
我用的是proxool连接池,application配置文件如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-lazy-init="true">
<!-- 数据库连接池配置文件 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName"
value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath*:/pool.properties</value>
</list>
</property>
</bean>
<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:component-scan base-package="org.wdj" />
<!-- 数据源配置,使用proxool连接池 -->
<bean id="dataSource"
class="org.logicalcobwebs.proxool.ProxoolDataSource"
destroy-method="close">
<!-- Connection Info -->
<property name="driver" value="${jdbc.driver}" />
<property name="driverUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Connection Pooling Info -->
<property name="maximumConnectionCount" value="500" />
<property name="minimumConnectionCount" value="12" />
<property name="maximumActiveTime" value="1000" />
</bean>
<bean id="defaultLobHandler"
class="org.springframework.jdbc.support.lob.DefaultLobHandler"
lazy-init="true" />
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:/sqlMapConfig.xml</value>
</property>
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="lobHandler">
<ref local="defaultLobHandler" />
</property>
</bean>
<!-- 数据源配置,使用应用服务器的数据库连接池 -->
<!--<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/ExampleDB" />-->
<!-- 事务管理器配置,单数据源事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
pool.properties文件如下
#derby version database settings
jdbc.driver=org.gjt.mm.mysql.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/html?useUnicode=true&characterEncoding=gbk
jdbc.username=root
jdbc.password=root
dao中方法如下
protected boolean batchSave(final String sqlId, final List<T> entityList) {
boolean flag = false;
int batch = 0;
try {
sqlMapClient.startBatch();
//设置事务不自动提交
sqlMapClient.getDataSource().getConnection().setAutoCommit(false);
for (T t : entityList) {
sqlMapClient.insert(sqlId, t);
batch++;
//这里打印的还是true
System.out.println("==========================" + batch+sqlMapClient.getDataSource().getConnection().getAutoCommit());
if (batch == 500) {
sqlMapClient.executeBatch();
batch = 0;
}
}
sqlMapClient.executeBatch();
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}