`
haihai
  • 浏览: 72219 次
  • 性别: Icon_minigender_1
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

Spring声明式事务学习

阅读更多
快过年了,没什么事情,看了一下spring2.0中的声明式的事务处理,自己做了一个小例子,是JTA方式的。
dataAccessContext-jta.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:jee="http://www.springframework.org/schema/jee"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">


    <jee:jndi-lookup id="DataSource" jndi-name="java:comp/env/jdbc/DataSource"/>

    <jee:jndi-lookup id="DataSource2" jndi-name="java:comp/env/jdbc/DataSource2"/>

	<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>

	<bean id="UserDao" class="dao.UserDaoImpl">
		<property name="ds" ref="DataSource"/>
		<property name="ds2" ref="DataSource2"/>
	</bean>


</beans>


applicationContext.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: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.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">


	<aop:config>
		<aop:advisor pointcut="execution(* *..UserDao.*(..))" advice-ref="txAdvice"/>
	</aop:config>

	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="insert*" rollback-for="java.sql.SQLException"/>
			<tx:method name="update*"/>		
			<tx:method name="*" read-only="true"/>
		</tx:attributes>
	</tx:advice>

</beans>
分享到:
评论
2 楼 haihai 2007-02-13  
事务的回滚和边界的划分是正确的,最后回滚的时候有连接泄漏,可能是抛出的SQLException的问题,如果是抛出的是其他的业务层的异常的话,我想就不会有连接泄漏的问题,我测试的时候一时也想不到什么好的业务异常,就弄了个SQLException,试了一下。在这一点上,是比ejb要好,检查异常,也能回滚事务。
1 楼 haihai 2007-02-13  
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app PUBLIC
	"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
	"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/dataAccessContext-jta.xml  /WEB-INF/applicationContext.xml
		</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>

	<resource-ref>
		<res-ref-name>jdbc/DataSource</res-ref-name>
		<res-type>javax.sql.DataSource</res-type>
		<res-auth>Container</res-auth>
	</resource-ref>

	<resource-ref>
		<res-ref-name>jdbc/DataSource2</res-ref-name>
		<res-type>javax.sql.DataSource</res-type>
		<res-auth>Container</res-auth>
	</resource-ref>

</web-app>


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

public interface UserDao {

    public void insertUserAndAddress(List list)throws SQLException;
}


UserDaoImpl.java
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

public class UserDaoImpl implements UserDao {

    private DataSource ds;

    private DataSource ds2;

    public void insertUserAndAddress(List list) throws SQLException {

	Connection conn = null;
	Connection conn2 = null;
	Connection conn3 = null;
	Connection conn4 = null;
	PreparedStatement pre_stmt = null;
	// PreparedStatement pre_stmt2 = null;
	PreparedStatement pre_stmt3 = null;
	PreparedStatement pre_stmt4 = null;

	conn = ds.getConnection();
	conn2 = ds.getConnection();
	conn3 = ds2.getConnection();
	conn4 = ds2.getConnection();

	pre_stmt = conn
		.prepareStatement("update scott.user_info set password=?");
	String ss = (String) list.get(0);
	pre_stmt.setString(1, ss);
	pre_stmt.executeUpdate();

	ss = (String) list.get(1);
	pre_stmt3 = conn4
		.prepareStatement("update scott.address set user_id=? where id=1");
	pre_stmt3.setInt(1, Integer.parseInt(ss));
	pre_stmt3.executeUpdate();

	// 人为的制造错误,id是数字类型
	ss = (String) list.get(2);
	String name = (String) list.get(3);
	String password = (String) list.get(4);
	pre_stmt4 = conn2
		.prepareStatement("insert into scott.user_info(id,name,password) values(?,?,?)");
	pre_stmt4.setString(1, ss);
	pre_stmt4.setString(2, name);
	pre_stmt4.setString(3, password);
	pre_stmt4.executeUpdate();

	pre_stmt.close();
	// pre_stmt2.close();
	pre_stmt3.close();
	pre_stmt4.close();
	conn.close();
	conn2.close();
	conn3.close();
	conn4.close();

    }

    public DataSource getDs() {
	return ds;
    }

    public void setDs(DataSource ds) {
	this.ds = ds;
    }

    public DataSource getDs2() {
	return ds2;
    }

    public void setDs2(DataSource ds2) {
	this.ds2 = ds2;
    }

}

相关推荐

    spring学习笔记(十六)-声明式事务的例子

    在本篇“spring学习笔记(十六)-声明式事务的例子”中,我们将深入探讨这一主题。 首先,声明式事务管理基于AOP(面向切面编程)实现,Spring通过代理模式在方法调用前后自动插入事务管理的代码。它主要通过两种方式...

    spring声明式事务管理+jdbc+连接池.zip

    本资料包"spring声明式事务管理+jdbc+连接池.zip"显然是针对Spring框架在数据库操作方面的深入学习,特别是如何利用Spring进行声明式事务管理和JDBC操作,以及如何配置和使用数据库连接池。接下来,我们将详细探讨这...

    spring+ibatis声明式事务Demo_

    通过这个Demo,开发者可以学习如何在Spring与iBatis集成的环境中配置和使用声明式事务,理解事务的ACID属性(原子性、一致性、隔离性和持久性)如何在实际项目中得到保障。此外,这也是理解和实践Spring AOP以及依赖...

    声明式事务控制spring+hibernate集成

    在"声明式事务控制,spring2.5+hibernate3集成源码"中,开发者可以学习如何配置Spring的事务管理器,以及如何在Hibernate的SessionFactory和SessionFactoryBuilder上使用Spring的TransactionProxyFactoryBean来创建...

    spring 自定义事务管理器,编程式事务,声明式事务@Transactional使用

    本教程将深入探讨如何在Spring中实现自定义事务管理器、编程式事务处理以及声明式事务`@Transactional`的使用。 首先,让我们了解事务管理的基本概念。事务是一组数据库操作,这些操作要么全部执行,要么全部回滚,...

    Spring学习笔记之九--声明式事务

    本篇文章将深入探讨Spring框架的一个关键特性——声明式事务管理。通过声明式事务,开发者可以避免手动处理事务的繁琐工作,提高代码的可读性和可维护性。 首先,我们了解什么是事务。在数据库操作中,事务是一系列...

    全面分析Spring的编程式事务管理与声明式事务管理.doc

    全面分析 Spring 的编程式事务管理与声明式事务管理 本文将从 Spring 的事务管理入手,深入讲解编程式事务管理和声明式事务管理的实现机制和原理。通过本文的学习,您将能够理解 Spring 事务管理的本质,并灵活运用...

    spring AOP(声明式事务管理)小程序

    在本“spring AOP(声明式事务管理)小程序”中,我们将深入探讨Spring AOP如何实现声明式事务管理,以及相关的通知类型。 1. **什么是声明式事务管理**: 声明式事务管理是相对于编程式事务管理而言的,后者需要在...

    spring_声明式事务_xml事务的书写.zip

    本教程将深入探讨Spring声明式事务以及如何通过XML配置来管理事务。声明式事务是Spring框架的一大亮点,它允许开发者无需在业务代码中显式处理事务,而是通过配置来控制事务的边界。 首先,我们要理解什么是事务。...

    Spring的声明式事务.doc

    本教程旨在深入讲解 Spring 的事务管理功能,包括编程式事务和声明式事务。通过学习本教程,您将能够理解 Spring 事务管理的本质,并灵活运用之。 一、事务管理的重要性 事务管理对于企业应用而言至关重要。它保证...

    spring学习事务源码

    首先,Spring事务管理有两种主要模式:编程式事务管理和声明式事务管理。编程式事务管理通过调用`PlatformTransactionManager`接口提供的方法进行显式控制,如`beginTransaction()`, `commit()`, 和`rollback()`。...

    跟我学Spring3(9.4)Spring的事务之声明式事

    Spring框架是Java开发中的核心组件,特别是在企业级应用中,它的事务管理能力是不可或缺的。在本教程"跟我学Spring3(9.4...通过本教程的学习,你将能够熟练地在Spring中应用声明式事务,提升你的开发效率和代码质量。

    spring学习笔记(十五)-编程式事务例子

    在实际开发中,我们通常使用声明式事务管理,它基于AOP(面向切面编程)来简化事务处理。然而,有时为了更细粒度的控制或者在特定场景下,我们可能需要采用编程式事务管理。 首先,了解事务的四大特性(ACID)是必...

    声明式事务处理(源代码)

    本资源包含两个文件:“Spring声明式事务处理.wrf”和“testtrans”,很可能是示例代码或者测试用例,用于演示如何在Java应用中使用Spring进行声明式事务处理。 首先,让我们深入理解声明式事务处理的概念。声明式...

    spring hibernate 事务管理学习笔记(一)

    接下来,我们将深入讨论Spring中的声明式事务管理。在Spring配置文件中,我们需要开启事务管理器,并在需要事务控制的Service层方法上添加`@Transactional`注解。这个注解允许我们指定事务的传播行为(如REQUIRED、...

    Spring_tx事务

    在Spring中,事务管理分为编程式事务管理和声明式事务管理两种方式。 **编程式事务管理**是通过编码来控制事务的开始、提交、回滚等过程。这通常使用`PlatformTransactionManager`接口实现,例如`...

    spring3+hibernate4配置声明式事务管理(annotation方式)

    通过这个小实例工程,我们可以学习到如何在Spring 3和Hibernate 4中利用注解实现声明式事务管理,这对于提升代码的可读性和可维护性至关重要。在实际项目中,这样的配置和使用方式非常常见,也是Java企业级开发的...

    spring框架的学习--事务

    - **基于XML的声明式事务管理**:在Spring的配置文件中,使用`&lt;tx:advice&gt;`、`&lt;aop:config&gt;`和`&lt;bean&gt;`标签定义事务规则,如事务的传播行为、隔离级别、超时设置等。 - **基于注解的声明式事务管理**:通过在...

    spring.net 声明式事物管理小示例

    Spring.NET 是一个基于 .NET Framework 的轻量...通过这个小示例,你将学习如何在 Spring.NET 中设置声明式事务管理,以及如何通过配置和注解控制事务的行为。这有助于创建更稳定、可扩展和易于维护的 .NET 应用程序。

Global site tag (gtag.js) - Google Analytics