当前位置:编程学习 > XML/UML >>

Spring入门Blog[十三、Spring中Xml声明事务]

补充知识点:

事务的传播特性:在当前的执行环境中。如果有多个方法嵌套互相调用的话,那么事务的特性必将从第一个方法传播到第二个、第三个。。。。


[java] 
/*事务的传播性。如果当前执行环境中有事务,那么则会一直在
   * 传播环境中传播下去。如果没有事务那么则会创建一个事务
   * 默认就是required
   * 而readOnly属性则是规定该事务只能是一些select操作。而insert,update等
   * 动作是不能执行的。因为readonly的限制,而且拿到readonly=true的transation
   * 的话它的执行效率是比前者高的
   * 还有更多事务配置请在:
   *http://static.springsource.org/spring/docs/2.5.6/reference/transaction.html#transaction-strategies
   * 9.5.6.1. @Transactionalsettings 中查看
  */ 
  @Transactional(propagation=Propagation.REQUIRED,readOnly=true) 
  public void save(User u){ 
     u.setName("zhanglong"); 
     UserLog log = new UserLog(); 
     log.setMsg("useradded"); 
     userDaoImpl.save(u); 
     userLogDaoImpl.save(log); 
  } 
下面看一个xml配置事务的案例:

1、 编写业务逻辑操作的简单方法:

[java] 
@Component("userService") 
public class UserServiceImpl implements UserService{ 
     
    @Resource 
    private UserDao userDaoImpl; 
    @Resource 
    private UserLogDao userLogDaoImpl; 
 
    public UserLogDao getUserLogDaoImpl() { 
        return userLogDaoImpl; 
    } 
    public void setUserLogDaoImpl(UserLogDao userLogDaoImpl) { 
        this.userLogDaoImpl = userLogDaoImpl; 
    } 
    public UserDao getUserDaoImpl() { 
        return userDaoImpl; 
    } 
    public void setUserDaoImpl(UserDao userDaoImpl) { 
        this.userDaoImpl = userDaoImpl; 
    } 
     
    public void save(User u){ 
        u.setName("zhanglong"); 
        UserLog log = new UserLog(); 
        log.setMsg("user added"); 
        userDaoImpl.save(u); 
        userLogDaoImpl.save(log); 
    } 

1、  编写XML配置文件,将事务加上去:

下面的文件主要包括配置数据库和指定事务前一段不用看。我们主要看事务在xml中的配置

1、  配置事务管理器transactionManager

2、 配置事务要管理的方法 tx:advice

3、 配置AOP将逻辑事务织入到相应的方法上去


[html]
<?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: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/context 
           http://www.springframework.org/schema/context/spring-context-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"> 
    <context:annotation-config /> 
    <!-- 配置容器资源扫描的包 --> 
    <context:component-scan base-package="com.spring" /> 
    <!-- 将前面类写入容器 --> 
    <bean id="logInterceptor" class="com.spring.aop.LogInterceptor" /> 
 
    <!-- 
        配置数据源 <bean id="myDataSource" 
        class="org.apache.commons.dbcp.BasicDataSource" 
        destroy-method="close"> <property name="driverClassName" 
        value="com.mysql.jdbc.Driver"/> <property name="url" 
        value="jdbc:mysql://localhost:3306/sms"/> <property name="username" 
        value="root"/> <property name="password" value="root"/> </bean> 
    --> 
 
    <!-- placeholder 占位符 --> 
    <bean 
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
        <property name="locations"> 
            <value>classpath:jdbc.properties</value> 
        </property> 
    </bean> 
    <!-- 配置dataSource --> 
    <bean id="dataSource" destroy-method="close" 
        class="org.apache.commons.dbcp.BasicDataSource"> 
        <property name="driverClassName" value="${jdbc.driverClassName}" /> 
        <property name="url" value="${jdbc.url}" /> 
        <property name="username" value="${jdbc.username}" /> 
        <property name="password" value="${jdbc.password}" /> 
    </bean> 
 
    <!-- 将配置好的dataSource注入到SessionFactory中--> 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
 &n

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,