-
spring,ibatis整合后的事务问题25
SPRING事务不回滚的问题,我进行了两次插入数据库的操作,第二次故意给一个非空字段插入null,但第一条记录还是成功插入了,一个星期了,没搞定,求指教。其中数据库已经设置为autocommit为false
框架及版本:
struts2 -2.0.1.4 , spring -2.5 ,ibatis 2.3,mysql 5.0
aplicationContext.xml:
<?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"> <bean id="dao" class="last.soul.dao.DAOImpl"> <property name="sqlMapClient" ref="sqlMapClient"></property> </bean> <bean id="userService" class="last.soul.service.UserServiceImpl"> <property name="dao" ref="dao"></property> </bean> <bean id="login" class="last.soul.action.LoginAction"> <property name="userService" ref="userService"></property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation" value="WEB-INF/sqlMapConfig.xml" /> <property name="dataSource" ref="dataSource" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <!-- 利用spring的TransactionProxyFactoryBean去对事务进行自动管理 <bean id="daoTr" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref local="transactionManager" /> </property> <property name="target"> <list><ref local="userService" /></list> </property> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED,-Exception</prop> </props> </property> </bean> --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- 哪些方法有 事务的传播性--> <tx:attributes> <tx:method name="*" isolation="READ_COMMITTED" propagation="REQUIRED" rollback-for="java.lang.Exception"/> </tx:attributes> </tx:advice> <!-- 哪些类的方法参与了事务 --> <aop:config> <!-- execution(* com.east.spring.managerimpl.*.*(..))这个类的所有方法都用事务 --> <aop:pointcut id="allManagerMethod" expression="execution(* last.soul.service.UserServiceImpl.*.*(..))" /> <!-- 引用 pointcut 和 advice--> <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" /> </aop:config> </beans>
UserServiceImpl:
package last.soul.service;
import last.soul.common.beans.User;
import last.soul.dao.DAO;
public class UserServiceImpl implements UserService {
DAO dao;
public boolean login(String username, String password) throws Exception {
User u1=new User();
u1.setEmail("d@163.com");
u1.setPassword("ccc");
u1.setUsername("aaaa");
dao.insert("user.insert", u1);
User u2=new User();
u2.setEmail("d@163.com");
u2.setPassword("he");
u2.setUsername(null);
dao.insert("user.insert", u2);
return true;
}
public DAO getDao() {
return dao;
}
public void setDao(DAO dao) {
this.dao = dao;
}
public boolean isExist(String username) {
return false;
}
public Integer register(User record) {
return null;
}
}
DAOImpl:
package last.soul.dao;
import java.sql.SQLException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
public class DAOImpl extends SqlMapClientDaoSupport implements DAO{
@SuppressWarnings("unchecked")
public void insert(String sql,Object map) throws SQLException
{
this.getSqlMapClientTemplate().insert(sql, map);
}
}
问题补充:liwenjie 写道spring默认进行回滚的异常是uncheckedException,而lz的SQLException 是checkedexception,无法进行回滚所以需要进行声明
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="false" />
<tx:method name="*" rollback-for="SQLException"/>
</tx:attributes>
</tx:advice>
详情请参见 spring参考手册 回滚
我配的Exception不也一样而且我就是配置上了SQLException也是不回滚。
后来我又用了另外一个方法就是让他抛出我自己的异常,我让我自己的这个异常继承RuntimeException也是不回滚啊。2010年4月27日 08:21
3个答案 按时间排序 按投票排序
-
code="xml"]
<!-- ######### Database Section Start ##########-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>org.postgresql.Driver</value>
</property>
<property name="url">
<value>jdbc:postgresql://127.0.0.1:5432</value>
</property>
<property name="username">
<value>postgres</value>
</property>
<property name="password">
<value>qdyhtg</value>
</property>
</bean>
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>\WEB-INF\sqlmapConfig.xml</value>
</property>
<property name="useTransactionAwareDataSource">
<value>true</value>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<bean id="sqlMapClientTemplate"
class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="dataSource" ref="dataSource"></property>
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" ></tx:annotation-driven>
在方法里使用声明式事务,我是这么做的。2010年4月28日 17:13
-
我一般是采用这种方式进行配置cglib
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:sql-map-config-db2.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* *..*Service.*(..))" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>2010年4月27日 22:05
-
spring默认进行回滚的异常是uncheckedException,而lz的SQLException 是checkedexception,无法进行回滚所以需要进行声明
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="false" />
<tx:method name="*" rollback-for="SQLException"/>
</tx:attributes>
</tx:advice>
详情请参见 spring参考手册 回滚2010年4月27日 20:02
相关推荐
SPRING与IBATIS整合采用保留IBATIS事务,可自己控制回滚!
SPRING与IBATIS整合采用保留IBATIS事务,可自己控制回滚!
为了开发方便,打算换一个架构,自己整合了s2sibatis框架,采用spring事务管理数据库,喜欢的可以看看,有什么问题请多多指教!
NULL 博文链接:https://ymgjava.iteye.com/blog/1117624
struts2整合spring、ibatis学生信息管理系统。事务管理、分页处理
本演示示例主要使用目前最新,最流行技术Struts2.1 +Spring 2.5.1+ibatis2.3整合开发而成,这与我以前发布的版本中最大特色是整合了Spring2.5.1中的注解功能和半自动化工具ibatis技术,这是本示例的两大特色,简化了配置...
这个Project是关于ibatis 和 spring 框架整合的Demo。 需要向大家说明一下几个需要注意的地方: ...2.ibatis和spring的整合主要是spring的配置文件需要配置ibatis的相关配置,如:数据库连接及事务管理等。
本源码为基于SpringMVC与iBatis整合的图形化代码生成系统设计,共包含379个文件,其中java文件312个,ftl文件44个,xml文件4个,properties文件4个,md文件3个,html文件2个,dtd文件2个,ini文件2个,gif文件2个,...
springmvc整合ibatis示例。增加了事务管理,连接池使用c3p0
耗时3天,对Struts2+Ibatis+Spring3.0+JreeChart进行了完整整合 包括Spring3.0的事务配置 OSCache二级缓存的配置 log4j实现输出Sql到控制台 JfreeChart与Struts2,Spring3.0的整合 对一个简单的表实现查询,批量删除...
那么,我们就想将 MyBatis3 的支持添加 到 Spring3.0(参考 Spring Jira 中的问题)中。而不幸的是,Spring 3.0 的开发在 MyBatis 3.0 官方发布前就结束了。 因为 Spring 开发团队不想发布一个基于非发布版的 MyBatis 的...
基于springmvc与ibatis 整合的swt/jface实现一键化自动生成 model、dao、service代码 以及spring web配置文件,并实现了事务。此工具源码请关注https://gitee.com/00fly/springmvc_ibatis_plus
org.springframework.orm-3.0.0.M4.jar: 整合第三方的ORM框架,如hibernate,ibatis,jdo,以及spring的JPA实现 org.springframework.transaction-3.0.0.M4.jar: 为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和...
spring-orm.jar:整合第三方的orm实现,如hibernate,ibatis,jdo以及spring 的jpa实现 spring-oxm.jar:Spring对于object/xml映射的支持,可以让JAVA与XML之间来回切换 spring-messaging.jar: spring-test.jar:对...
org.springframework.orm-3.0.0.M4.jar: 整合第三方的ORM框架,如hibernate,ibatis,jdo,以及spring的JPA实现 org.springframework.transaction-3.0.0.M4.jar: 为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和...
9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.6.1. @Transactional 有关的设置 ...
4.4 用Spring整合Hibernate 4.4.1 Hibernate概览 4.4.2 管理Hibernate资源 4.4.3 用HibernateTemplate访问Hibernate 4.4.4 HibernateDaoSupport的子类 4.5 Spring和JDO 4.5.1 配置JDO ...
9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 插入事务操作 9.5.8. ...
9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. <tx:advice/> 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 插入事务操作 9.5.8. ...
其他还有一些特点不是Spring的核心,这里只做简单陈述,如:对JDBC的封装与简化,提供事务管理功能,对O/R mapping工具(hibernate、iBATIS)的整合;提供MVC解决方案,也可以与其他web框架(Struts、JSF)进行整合...