Spring MVC使用動態代理實現事務控制
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;
}