Spring MVC使用动态代理实现事务控制
2013-05-21 22:14 阅读(187)

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:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
	default-lazy-init="true">
	<!--
		spring在启动的时候,会默认加载会默认加载整个对象实例图,从初始化ACTION配置、到
		service配置到dao配置、乃至到数据库连接、事务等等。这样可以减少web服务器在运行时的负担,但是对于开发者来说无疑是效率极低的一个设置了。
		还好,spring提供了default-lazy-init属性,其配置形式如下,applicationContext.xml中: <
		beans default-lazy-init ="true" > < bean class ="org.xxxx.bean" >
		。。。。。。 </beans>
		spring配置默认default-lazy-init为false,当配置为true时sping不会再去加载整个对象实例图,大大减少了初始化的时间,减少了spring的启动速度。
		这样做只是为了在开发过程中节约启动时间,在部署到实际环境中,倒是没必要设置default-lazy-init为true。毕竟部署到实际环境中不是经常的事,每次启动1分钟倒不是大问题,而且可以提高服务器效率。
		当然,也不是所有的beans都能设置default-lazy-init成为true.对于scheduler的bean不能用lazy-init
		< beans default-lazy-init ="true" > < bean class
		="org.springframework.scheduling.quartz.SchedulerFactoryBean" > <
		property name ="triggers" > < list > < ref bean ="buildHtmlTrigger" />
		< ref bean ="askTrigger" /> < ref bean ="mailSenderTrigger" /> < ref
		bean ="topicDetailBuildTrigger" /> < ref bean ="forumBuildTrigger" />
		< ref bean ="topicBuildTrigger" /> </ list > </ property > </ bean >
		</ beans > 这样的话。所有的scheduler就都不管用了。所以请大家要注意。
	-->
 
	<!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
	<context:component-scan base-package="com.edufe">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
	<context:property-placeholder
		ignore-resource-not-found="true"
		location="classpath*:/application.properties,
          			  classpath*:/application.development.properties" />
	<!-- 创建数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	<!-- 使用嵌入式数据库H2 -->
	<!--
		<jdbc:embedded-database id="dataSource" type="H2"> <jdbc:script
		location="classpath:sql/h2/schema.sql" /> <jdbc:script
		location="classpath:sql/h2/import-data.sql" />
		</jdbc:embedded-database>
	-->
	<!-- 创建jdbcTemplate对象 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 在容器文件中配置bean(service,dao,domain,action,数据源), -->
	<!--
		bean的作用是, 当我们spring框架加载的时候,spring就会自动创建一个bean,并放入内存 即产生UserService
		user=new UserService(); user.setName("张三");
	-->
	<!--
		<bean id="userService" class=""> 这里就体现出了注入的概念 <property name="name">
		<value>张三</value> </property> 在UserService中引用ByeService的对象ref是个引用
		<property name="byeS" ref="byeService" /> </bean>
	-->
 
 
	<!-- 处理事务 -->
	<!-- 生成一个事务管理对象 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<constructor-arg index="0" ref="dataSource">
		</constructor-arg>
	</bean>
	
	<!-- 生成默认事务定义对象 -->
	<bean id="def" class="org.springframework.transaction.support.DefaultTransactionDefinition"></bean>
 
</beans>
在dao中

@Autowired
	private DataSourceTransactionManager transactionManager;
	@Autowired
	private DefaultTransactionDefinition def;
public int excuteTrac() {
		int temp = 0;
		// 批插入
		String sql1[] = new String[4];
		// 向第一个表插入的语句
		sql1[0] = "insert into usermbo( ID, USERNAME, age) values('122','22','22')";
		sql1[1] = "insert into usermbo( ID, USERNAME, age) values('133','33','33')";
		sql1[2] = "insert into usermbo( ID, USERNAME, age) values('144','44','33')";
		sql1[3] = "insert into usermbo( ID, USERNAME, age) values('155','55','33')";
		String[] sql2 = new String[3];
		// 向第二个表插入的语句
		sql2[0] = "insert into address (NO, NAME) values('33','33')";
		// 此条数据是错误数据 插入会出现异常
		sql2[1] = "insert into address (NO, NAME)  values('eee','44')";
		sql2[2] = "insert into address (NO, NAME)  values('144','44')";
 
		TransactionStatus status = transactionManager.getTransaction(def);
		try {
			int[] a = jdbcTemplate.batchUpdate(sql1);
			int[] b = jdbcTemplate.batchUpdate(sql2);
			try {
				transactionManager.commit(status);
			} catch (Exception e) {
				 System.out.println("事务提交异常");
			}
		} catch (Exception ex) {
			 System.out.println("出现事务异常");
			try {
				transactionManager.rollback(status);
			} catch (IllegalTransactionStateException e) {
				 System.out.println("回滚数据异常");
			}
			temp = -1;
		}
		return temp;
	}