-
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
相关推荐
1. 添加依赖:在`spring ibatis整合所需jar包`中,通常包含以下关键jar文件: - spring核心库:如spring-context、spring-beans、spring-aop等,用于Spring框架的基本功能。 - spring-jdbc和spring-tx:支持Spring...
当保留ibatis事务后,开发者可以在业务逻辑中更自由地控制事务的提交和回滚。例如,在某个服务方法中,可以通过显式调用ibatis的`commit`和`rollback`方法来控制事务的最终状态。这种方式适用于那些需要更加精细控制...
在IT行业中,Spring框架与iBatis(现为MyBatis)的整合是常见的数据访问层解决方案,尤其在企业级应用开发中极为普遍。Spring是一个全面的Java应用程序框架,提供了依赖注入、AOP(面向切面编程)、MVC(模型-视图-...
7. **DAO 层实现**:在整合后,可以创建基于 iBATIS 的 DAO 类,这些类继承自 SqlSessionDaoSupport 或使用 SqlSessionTemplate,通过这些工具类与数据库进行交互。 8. **Service 层设计**:在 Service 层,我们...
7. **测试**:在Spring与iBatis整合后,可以使用Spring的`@Transactional`注解进行单元测试,确保每个方法都在独立的事务中执行,便于模拟各种数据状态并进行断言。 以上就是关于"Spring 3.2与iBatis整合"及"在...
当我们把Spring、Struts2和iBatis整合在一起时,可以构建出一个高效、模块化的Web应用。Spring作为整体的框架容器,负责管理所有的Bean,包括Struts2和iBatis的相关组件。Struts2处理HTTP请求,调用Spring管理的业务...
根据提供的文件信息,本文将详细解析Spring与iBatis整合时如何保留并使用iBatis事务管理机制,以及如何在应用程序中实现手动控制事务的方法。 ### Spring与iBatis整合 Spring框架是一个全面的企业级应用开发框架,...
"Struts2+Spring+iBatis整合"是一个典型的MVC(Model-View-Controller)架构实现,适用于构建大型企业级应用。下面将详细介绍这三个框架以及它们整合的关键点。 **Struts2** 是一个基于MVC设计模式的Web应用框架,...
Spring3 和 iBatis 整合是一个常见的Java企业级应用开发模式,主要用于构建灵活、可扩展的后端数据访问层。这个项目实例提供了一个详细的学习资源,帮助新手掌握这两种技术的融合。以下是对这个整合实例的详细解析:...
本篇将详细介绍如何在Spring 2.5版本中整合iBATIS 2.3,并利用Spring的声明式事务管理,以提升应用程序的稳定性和可维护性。 首先,我们需要了解Spring 2.5和iBATIS 2.3的基本概念。Spring 2.5是Spring框架的一个...
### Spring与iBatis整合详解 #### 一、前言 在Java企业级应用开发中,Spring框架以其强大的依赖注入(DI)和面向切面编程(AOP)能力深受开发者喜爱,而iBatis(现更名为MyBatis)作为一款优秀的持久层框架,通过...
Struts2、Spring和iBatis是Java Web开发中常用的三个框架,它们分别负责MVC模式中的Action层、业务逻辑层以及数据访问层。将这三个框架整合在一起,可以构建出高效、灵活的企业级应用。 **Struts2** 是一个基于MVC...
在整合Spring和iBatis框架时,我们需要进行一系列的配置工作,以便于让Spring负责管理iBatis的数据访问层。Spring作为一个强大的IoC(Inversion of Control)和AOP(Aspect Oriented Programming)容器,可以方便地...
1. 添加依赖:整合SSH,需要在项目中引入相应的jar包,包括Struts2、Spring、iBatis以及它们的依赖库,例如sitemesh用于页面装饰。确保包含所有必要的jar,如描述中提到的,这里有18个jar包,涵盖了基本需求。 2. ...
#### 一、Spring与IBatis整合概述 Spring框架与IBatis(现称为MyBatis)的整合为开发者提供了一种更简洁、更强大的数据库访问方式。Spring通过其内置的支持机制极大地简化了原有的IBatis访问代码,并且提供了与...
在与Struts2和iBatis的整合中,Spring主要负责管理Bean的生命周期和依赖关系,以及事务的统一管理。Spring的事务管理可以基于编程式或者声明式,后者通过@Transactional注解实现,更符合开闭原则,降低了代码的耦合...
### ibatis与Spring框架整合详解 #### 一、ibatis简介 ibatis是一个开源的、基于Java的持久层框架,它提供了SQL映射的方式来进行数据库访问。与Hibernate等其他ORM框架相比,ibatis更加轻量级,对于那些只需要简单...
5. **事务管理**:Spring的PlatformTransactionManager接口负责处理事务,通过配置,可以将iBatis的数据库操作纳入Spring的事务管理范围。这样,当发生异常时,可以自动回滚事务,保证数据的一致性。 6. **DAO层...
通过Struts2、Spring和iBatis的整合,我们可以实现模型、视图和控制的分离,提高代码的可读性和可维护性。同时,Spring的DI和AOP特性使得对象管理更加灵活,iBatis则提供了方便的数据库操作方式。这样的组合在企业级...
Struts2、Spring和iBatis是Java Web开发中常用的三大框架,它们分别负责MVC模式中的动作控制、依赖注入及持久层操作。本项目整合了这三个框架,并使用MySQL作为数据库,实现了一个基础的用户登录注册查询删除的功能...