最近做个批处理的项目,用的是spring2.5+iBATIS2做框架
用的是iBATIS回调函数的方式做的批处理,但是遇到了一个问题做了批量插入后,数据库却没有反应
不知为什么,请高手帮忙看看
还有一个问题就是用回调函数的方式,事物是不是不用手工启动。我在spring里把jdbc的自动提交设置成了FALSE
spring配置文件:<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "
default-autowire="byName"><!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<!--
<property name="url" value="jdbc:sqlserver://172.16.5.48:1433;DatabaseName=TAMS"/>
<property name="username" value="sa"/>
<property name="password" value="TEST"/>
--> <property name="url" value="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=TAMS;"/>
<property name="username" value="ad"/>
<property name="password" value="ad"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
<property name="defaultAutoCommit" value="false"/>
</bean>
<!-- 配置事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置业务bean -->
<!-- 采用@Transactional注解方式来使用事务
<tx:annotation-driven transaction-manager="txManager"/> --> <bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"
value="classpath:sqlmap-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="baseTxService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager" ref="txManager" />
<property name="proxyTargetClass" value="true" />
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="query*">readOnly</prop>
<prop key="get*">readOnly</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <bean id="textDao" class="com.strike.java.core.db.daoImpl.TextDaoImpl">
<property name="dataSource" ref="dataSource"></property>
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
</beans>
daoimpl
/**
*
*/
package com.strike.java.core.db.daoImpl;import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.strike.java.core.db.dao.TextDao;
import com.strike.java.core.db.daoBean.TextDaoBean;
import com.strike.java.core.pattern.db.BaseBatch;/**
* @author gaoweizhuang
*
*/
public class TextDaoImpl extends SqlMapClientDaoSupport implements TextDao { /* (non-Javadoc)
* @see com.strike.java.core.db.dao.TextDao#text()
*/
@Override
public int text() throws SQLException {
// TODO Auto-generated method stub
final List<TextDaoBean> parm = new ArrayList();
TextDaoBean textDaoBean = new TextDaoBean();
textDaoBean.setUsername("2");
textDaoBean.setPassword("2");
parm.add(textDaoBean); // 初始化
final int size = 0; // 执行批处理操作模板
getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor)
throws SQLException {
System.out.println("当前事物状态: " + getSqlMapClient().
getDataSource().getConnection().getAutoCommit());
// 开启事物
getSqlMapClient().startTransaction();
// 开始插入批处理
executor.startBatch();
// 变量集合
Iterator<TextDaoBean> iter = parm.iterator(); // 日志记录批量插入开始
logger.info("batch start:");
// 循环操作
// while(iter.hasNext()){
// // 插入数据
// executor.insert("BC_MST_PREF_SqlMap.insertUser", parm.get(0));
// }
TextDaoBean textDaoBean = iter.next();
executor.insert("BC_MST_PREF_SqlMap.insertUser", textDaoBean);
// 执行批处理
executor.executeBatch();
// 执行事物
getSqlMapClient().commitTransaction();
// 结束事物
getSqlMapClient().endTransaction(); // 日志记录批处理结束
logger.info("本次批处理插入数据" + size + "条"); // 返回处理条数
return size;
}
});
// log结束
logger.info("batch end:");
return size;// super.insertBatch(parm);
}}
sqlxml:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="BC_MST_PREF_SqlMap">
<typeAlias alias="TextDaoBean" type="com.strike.java.core.db.daoBean.TextDaoBean"/>
<resultMap id="resultMap" class="com.strike.java.core.db.daoBean.TextDaoBean">
<result property="username" column="USERNAME" columnIndex="1"/>
<result property="password" column="PASSWORD" columnIndex="2"/>
</resultMap>
<insert id="insertUser" parameterClass="TextDaoBean">
insert into t_user (USERNAME,PASSWORD) values (#username#,#password#)
</insert></sqlMap>
用的是iBATIS回调函数的方式做的批处理,但是遇到了一个问题做了批量插入后,数据库却没有反应
不知为什么,请高手帮忙看看
还有一个问题就是用回调函数的方式,事物是不是不用手工启动。我在spring里把jdbc的自动提交设置成了FALSE
spring配置文件:<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "
default-autowire="byName"><!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<!--
<property name="url" value="jdbc:sqlserver://172.16.5.48:1433;DatabaseName=TAMS"/>
<property name="username" value="sa"/>
<property name="password" value="TEST"/>
--> <property name="url" value="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=TAMS;"/>
<property name="username" value="ad"/>
<property name="password" value="ad"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
<property name="defaultAutoCommit" value="false"/>
</bean>
<!-- 配置事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置业务bean -->
<!-- 采用@Transactional注解方式来使用事务
<tx:annotation-driven transaction-manager="txManager"/> --> <bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"
value="classpath:sqlmap-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="baseTxService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager" ref="txManager" />
<property name="proxyTargetClass" value="true" />
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="query*">readOnly</prop>
<prop key="get*">readOnly</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <bean id="textDao" class="com.strike.java.core.db.daoImpl.TextDaoImpl">
<property name="dataSource" ref="dataSource"></property>
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
</beans>
daoimpl
/**
*
*/
package com.strike.java.core.db.daoImpl;import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.strike.java.core.db.dao.TextDao;
import com.strike.java.core.db.daoBean.TextDaoBean;
import com.strike.java.core.pattern.db.BaseBatch;/**
* @author gaoweizhuang
*
*/
public class TextDaoImpl extends SqlMapClientDaoSupport implements TextDao { /* (non-Javadoc)
* @see com.strike.java.core.db.dao.TextDao#text()
*/
@Override
public int text() throws SQLException {
// TODO Auto-generated method stub
final List<TextDaoBean> parm = new ArrayList();
TextDaoBean textDaoBean = new TextDaoBean();
textDaoBean.setUsername("2");
textDaoBean.setPassword("2");
parm.add(textDaoBean); // 初始化
final int size = 0; // 执行批处理操作模板
getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor)
throws SQLException {
System.out.println("当前事物状态: " + getSqlMapClient().
getDataSource().getConnection().getAutoCommit());
// 开启事物
getSqlMapClient().startTransaction();
// 开始插入批处理
executor.startBatch();
// 变量集合
Iterator<TextDaoBean> iter = parm.iterator(); // 日志记录批量插入开始
logger.info("batch start:");
// 循环操作
// while(iter.hasNext()){
// // 插入数据
// executor.insert("BC_MST_PREF_SqlMap.insertUser", parm.get(0));
// }
TextDaoBean textDaoBean = iter.next();
executor.insert("BC_MST_PREF_SqlMap.insertUser", textDaoBean);
// 执行批处理
executor.executeBatch();
// 执行事物
getSqlMapClient().commitTransaction();
// 结束事物
getSqlMapClient().endTransaction(); // 日志记录批处理结束
logger.info("本次批处理插入数据" + size + "条"); // 返回处理条数
return size;
}
});
// log结束
logger.info("batch end:");
return size;// super.insertBatch(parm);
}}
sqlxml:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="BC_MST_PREF_SqlMap">
<typeAlias alias="TextDaoBean" type="com.strike.java.core.db.daoBean.TextDaoBean"/>
<resultMap id="resultMap" class="com.strike.java.core.db.daoBean.TextDaoBean">
<result property="username" column="USERNAME" columnIndex="1"/>
<result property="password" column="PASSWORD" columnIndex="2"/>
</resultMap>
<insert id="insertUser" parameterClass="TextDaoBean">
insert into t_user (USERNAME,PASSWORD) values (#username#,#password#)
</insert></sqlMap>
解决方案 »
- jfreeschars可以画曲线图吗?
- hibernate 操作数据库的奇怪问题
- 求助java ftp上传组件
- bea weblogic 9.2 portal开发,有没有相关开发实例教程文档 ,请各位大侠指教,谢谢!
- 求jfreechart-0.9.21或jfreechart-0.9.20开发文档。
- 一个java初学者的代码求助!先谢谢各位大侠!
- JAVASCRIPT高手进~~~~~~~~~~~~
- 散100分问一个简单问题?
- 已经很久了,db2显示date类型的字段值的问题
- (救急!!!!!)windows nt+tomcat+apache里,怎样连接数据库(给50分)
- 求助:jsp页面 根据word模板生成word
- [JSF]如何在HttpSessionListener中获取managedBean
new FileSystemXmlApplicationContext("src/applicationContext.xml");
spring配置文件那你好像没有提交吧?
把JDBC的defaultAutoCommit设置FALSE是为了防止批处理一个一个提交,而非批量执行
换句话说defaultAutoCommit设置成TRUE时跟一条一条执行语句是一样的,批处理就没有意义了
iBATIS用回调函数管理批处理的时候是怎么手动commit的?
还有就是一个问题就是本身spring就建立了一个全局事物,iBATIS手工再建立个事物是不是会有问题
SqlMapClient sqlMapClientTo = null;
try {
final ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-synchro.xml");
final SynchroCustomerFromDAOImpl synchroCustomerFromDao = (SynchroCustomerFromDAOImpl) ctx.getBean("synchroCustomerFromDao");
final SynchroCustomerToDAOImpl synchroCustomerToDao = (SynchroCustomerToDAOImpl) ctx.getBean("synchroCustomerToDao");
//get memberList from oracle
List<MemberModel> memberListFrom = synchroCustomerFromDao.selectBillmembertList(); sqlMapClientTo = synchroCustomerToDao.getSqlMapClient();
//设置自动提交为false,在配置文件中设置视乎有不能提交的问题
sqlMapClientTo.getDataSource().getConnection().setAutoCommit(false);
//开始事物:startTransaction()和startBatch()要同时调用
sqlMapClientTo.startTransaction();
sqlMapClientTo.startBatch();
//在一个事物中要同时处理删除和插入
for (MemberModel memberModel : memberListFrom) {
//delete memberList
sqlMapClientTo.delete("deleteBillmembert", new String(memberModel.getMemberId().getBytes("ISO-8859-1"),"MS932"));
//insert memberList
memberModel.setMemberId(new String(memberModel.getMemberId().getBytes("ISO-8859-1"),"MS932"));
sqlMapClientTo.insert("insertBillmembert",memberModel);
}
//结束事物:executeBatch()和commitTransaction()要同时调用
sqlMapClientTo.executeBatch();
sqlMapClientTo.commitTransaction();
} catch (final Exception e) {
e.printStackTrace();
}finally{
try {
sqlMapClientTo.endTransaction();
} catch (SQLException e) {
e.printStackTrace();
}
} }