1.java代碼:
//Foo.javapackage x.y.service;public class Foo {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}//FooService.javapackage x.y.service;//the service interface that we want to make transactionalpublic interface FooService {Foo getFoo(String fooName);Foo getFoo(String fooName, String barName);void insertFoo(Foo foo);void updateFoo(Foo foo);}//FooServiceImpl.javapackage x.y.service;import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;import org.springframework.transaction.annotation.Transactional;public class FooServiceImpl extends SimpleJdbcDaoSupport implements FooService {@Overridepublic Foo getFoo(String fooName) {String sql = "insert into Foo values(5,'5555555555555555')";int cnt2=this.getSimpleJdbcTemplate().update(sql);System.out.println("affected2:"+cnt2);return null;}@Overridepublic Foo getFoo(String fooName, String barName) {// TODO Auto-generated method stubreturn null;}@Override@Transactionalpublic void insertFoo(Foo foo) {String sql = "insert into Foo values("+foo.getId()+",'"+foo.getName()+"')";int cnt=this.getSimpleJdbcTemplate().update(sql);int cnt2=this.getSimpleJdbcTemplate().update(sql);System.out.println("affected:"+cnt);System.out.println("affected2:"+cnt2);}@Overridepublic void updateFoo(Foo foo) {// TODO Auto-generated method stub}}//Test.javapackage x.y.service;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {/** * @param args */public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("applicatinContext.xml", Test.class);FooService fooService = (FooService) ctx.getBean("fooService");//fooService.getFoo("");Foo f1=new Foo();f1.setId(13);f1.setName("吳xx2222222222");fooService.insertFoo(f1);System.out.println("done");}}
2.Spring配置 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.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><!--此為增加 交易處理 的第1種配置方法 this is the service object that we want to make transactional --><!-- enable the configuration of transactional behavior based on annotations --><tx:annotation-driven transaction-manager="txManager" /><bean id="fooService" class="x.y.service.FooServiceImpl"><property name="dataSource" ref="dataSource" /></bean><!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) --><tx:advice id="txAdvice" transaction-manager="txManager"><!-- the transactional semantics... --><tx:attributes><!-- all methods starting with 'get' are read-only --><tx:method name="get*" read-only="true" /><!-- other methods use the default transaction settings (see below) --><tx:method name="*" /></tx:attributes></tx:advice><!-- ensure that the above transactional advice runs for any execution of an operation defined by the FooService interface --><!-- 此為增加 交易處理 的第2種配置方法<aop:config><aop:pointcut id="fooServiceOperation"expression="execution(* x.y.service.FooService.*(..))" /><aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/> </aop:config>--><!-- don't forget the DataSource --><!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@rj-t42:1521:elvis" /> <property name="username" value="scott" /> <property name="password" value="tiger" /> </bean> --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /><property name="url"value="jdbc:sqlserver://192.168.10.10:1433;databaseName=Test" /><property name="username" value="sa" /><property name="password" value="123" /></bean><!-- similarly, don't forget the PlatformTransactionManager --><bean id="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- other <bean/> definitions here --></beans>
3.sql
create table Foo(id bigint primary key,name nvarchar(50))
運行程式,當注釋spring配置的事務時,可以插入一條記錄,而配置好spring事務,不能插入記錄。