论坛首页 Java企业应用论坛

Spring声明式事务学习

浏览 7123 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-02-13  
快过年了,没什么事情,看了一下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>
   发表时间: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;
    }

}
0 请登录后投票
   发表时间:2007-02-13  
事务的回滚和边界的划分是正确的,最后回滚的时候有连接泄漏,可能是抛出的SQLException的问题,如果是抛出的是其他的业务层的异常的话,我想就不会有连接泄漏的问题,我测试的时候一时也想不到什么好的业务异常,就弄了个SQLException,试了一下。在这一点上,是比ejb要好,检查异常,也能回滚事务。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics