请高手来回答事务问题

strike2368168 2010-04-23 01:30:11
我现在的框架是spring+iBATIS
现在事务的问题把我搞的挺郁闷的,请高手帮忙解决一下,希望说出为什么,光说答案没有意义
现在是3个事物,一个是JDBC的事务,一个是iBATIS的事务,一个是spring的事务
1、这3个事务在这个项目是什么关系,我的理解是iBATIS本身是调JDBC的事务,JDBC本身默认的是自动提交的
现在的问题是在程序里调用iBATIS的事务,但是没有起作用感觉程序本身就没有加入事务似的。如果我把JDBC的自动提交设置为FALSE,程序就无法提交。
2、spring管理iBATIS的事务,在程序里再用iBATIS自己的事务管理还有用吗?
3、如果使用spring的事务管理,怎么在程序里实现手动进行事务管理
4、每个dao里都有事务,把dao注入到service里还有一个对所有dao的事务,这两个事务该怎么理解,是什么关系
希望大家帮我解答一下,困惑很长时间了
5、spring对iBATIS的集成,怎么管理dao的事务

我的代码:

<?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="sqlMapClientTemplate"
class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient" />
</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.TextDaoImpl2">
<property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"></property>
</bean>
</beans>



package com.strike.java.core.db.daoImpl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.strike.java.core.db.dao.TextDao;
import com.strike.java.core.db.daoBean.TextDaoBean;

public class TextDaoImpl2 implements TextDao {

private SqlMapClientTemplate sqlMapClientTemplate;

public SqlMapClientTemplate getSqlMapClientTemplate() {
return sqlMapClientTemplate;
}

public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
this.sqlMapClientTemplate = sqlMapClientTemplate;
}

@Override
public int text() throws SQLException {
List<TextDaoBean> parm = new ArrayList();

for(int i = 0;i < 15000; i++){
TextDaoBean textDaoBean = new TextDaoBean();
textDaoBean.setUsername(String.valueOf(i));
textDaoBean.setPassword(String.valueOf(i));
parm.add(textDaoBean);
}
SqlMapClient st = sqlMapClientTemplate.getSqlMapClient();
st.startTransaction();
st.startBatch();
long batchStartTime = System.currentTimeMillis();
System.out.println(batchStartTime);
for(int j = 0;j < parm.size(); j++){
st.insert("BC_MST_PREF_SqlMap.insertUser", parm.get(j));
}
st.executeBatch();
st.commitTransaction();
long batchEndTime = System.currentTimeMillis();
System.out.println(batchEndTime-batchStartTime);
st.endTransaction();
// TODO Auto-generated method stub
return 0;
}

}

...全文
131 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
裘马轻狂大师 2010-04-27
  • 打赏
  • 举报
回复
事务真有这么复杂吗
你用spring事务干嘛还要用其它本身的事务,有必要没
z2008g 2010-04-27
  • 打赏
  • 举报
回复
学习学习啦
wutian4567268 2010-04-27
  • 打赏
  • 举报
回复
手动开启一个事务,用try - catch语句,try语句最后提交事务,finally中结束事务。这样如果中间出现异常,事务就不会提交,直接结束
FlyingFish0912 2010-04-27
  • 打赏
  • 举报
回复
这个搭配还真没用过,帮顶了!
newplayerone 2010-04-23
  • 打赏
  • 举报
回复
spring+iBATIS 没有用过
SSH框架到是用过,个人看法:
如果你把所有的bean都注册到spring容器里面那么相应的应该是把所有的事务都交给spring托管也就没有其他什么hibernate事务,jdbc事务。当然你需要在spring的配值文件里面说明你的事务范围。可以管理到哪一层,对哪些操作进行管理,对哪些以某个固定名字开头结尾的类进行管理等等。这些在spring的AOP里面都可以配值。这也就意味着把那些一个个小的事务纳入到spring的事务管理中了。

以上只是自己的个人意见,期待高手的见解! 关注中!
lp19890601 2010-04-23
  • 打赏
  • 举报
回复
幫頂!!!!!!!!

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧