• java Spring 2.5.6 SimpleJdbcDaoSupport Transaction 事务验证示例


    1.java代码:
    //Foo.java
    
    package 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.java
    package x.y.service;
    
    //the service interface that we want to make transactional
    public interface FooService {
    	Foo getFoo(String fooName);
    
    	Foo getFoo(String fooName, String barName);
    
    	void insertFoo(Foo foo);
    
    	void updateFoo(Foo foo);
    }
    //FooServiceImpl.java
    package x.y.service;
    
    import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
    import org.springframework.transaction.annotation.Transactional;
    
    public class FooServiceImpl extends SimpleJdbcDaoSupport implements FooService {
    
    	@Override
    	public 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;
    	}
    
    	@Override
    	public Foo getFoo(String fooName, String barName) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	@Override
    	@Transactional
    	public 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);
    	}
    
    	@Override
    	public void updateFoo(Foo foo) {
    		// TODO Auto-generated method stub
    
    	}
    
    }
    
    //Test.java
    package 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.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://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事务,不能插入记录。
  • 相关阅读:
    *Convert Sorted Array to Binary Search Tree
    *Count Complete Tree Nodes
    *Binary Tree Paths
    Invert Binary Tree
    *Kth Smallest Element in a BST
    **Lowest Common Ancestor of Two Nodes in a Binary Tree
    Lowest Common Ancestor of a Binary Search Tree
    *Sum root to leaf number
    subversion javahl
    mongodb从来没有说它写成功了。
  • 原文地址:https://www.cnblogs.com/wucg/p/2082979.html
Copyright © 2020-2023  润新知