首页 > 代码库 > Spring——声明式的事务处理

Spring——声明式的事务处理

Spring声明式的事务管理

1、使用Annotation进行声明的事务管理

(1)此处以Hibernate声名式的事务管理

(2)事务应该加载业务逻辑层,即service

(3)假设现在要在业务逻辑层加上日志逻辑

a) 建立实体类Log,使用HibernateAnnotation,指定id:

package com.zgy.model;

 

import javax.persistence.Entity;

import javax.persistence.Id;

 

@Entity

public class Log {

private int id;

private String msg;

 

public String getMsg() {

return msg;

}

 

public void setMsg(String msg) {

this.msg = msg;

}

 

@Id

public int getId() {

return id;

}

 

public void setId(int id) {

this.id = id;

}

}

 

b) 定义LogDAO

package com.zgy.dao;

 

import com.zgy.model.Log;

 

public interface LogDAO {

public void saveLog(Log log);

}

 

c) 定义LogDAO的实现LogDAOImpl:

package com.zgy.impl;

 

import javax.annotation.Resource;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.stereotype.Component;

 

import com.zgy.dao.LogDAO;

import com.zgy.model.Log;

 

@Component("logDAO") 

public class LogDAOImpl implements LogDAO {

 

private SessionFactory sessionFactory;

 

public SessionFactory getSessionFactory() {

return sessionFactory;

}

@Resource(name="sessionFactory")

public void setSessionFactory(SessionFactory sessionFactory) {

this.sessionFactory = sessionFactory;

}

@Override

public void saveLog(Log log) {

Session s = sessionFactory.getCurrentSession();

s.save(log);

//throw new RuntimeException("error");

}

}

 

d) 在原来的业务逻辑上加上日志的逻辑:

package com.zgy.service;

 

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import org.springframework.transaction.annotation.Transactional;

 

import com.zgy.dao.LogDAO;

import com.zgy.dao.UserDAO;

import com.zgy.model.Log;

import com.zgy.model.User;

 

@Component("userService")

public class UserService {

private UserDAO userDAO; 

private LogDAO logDAO;

//声明式的事务处理

@Transactional

public void add(User user) {

userDAO.save(user);

Log log = new Log();

log.setMsg("a user saved");

logDAO.saveLog(log);

}

public UserDAO getUserDAO() {

return userDAO;

}

@Resource(name="u")

public void setUserDAO( UserDAO userDAO) {

this.userDAO = userDAO;

}

public LogDAO getLogDAO() {

return logDAO;

}

@Resource(name="logDAO")

public void setLogDAO(LogDAO logDAO) {

this.logDAO = logDAO;

}

}

 

e) 配置文件如下:

配置文件jdbc.properties定义连接数据库所需的相关信息

dataSource注入sessionFactory

sessionFactory注入txManager

 

<?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/aop

           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

           http://www.springframework.org/schema/tx 

           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<context:annotation-config />

<context:component-scan base-package="com.zgy" />

<bean

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<value>classpath:jdbc.properties</value>

</property>

</bean>

 

<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>

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="annotatedClasses">

<list>

<value>com.zgy.model.User</value>

<value>com.zgy.model.Log</value>

</list>

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">

org.hibernate.dialect.MySQLDialect

</prop>

<prop key="hibernate.show_sql">true</prop>

</props>

</property>

</bean>

 

 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

  <property name="sessionFactory" ref="sessionFactory"></property>

 </bean>

 

 <tx:annotation-driven transaction-manager="txManager"/>

</beans>

f) 取消被注释的代码,事务提交将出现异常,事务将自动回滚,而不会将数据插入数据库。

2、使用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: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/aop

           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

           http://www.springframework.org/schema/tx 

           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<context:annotation-config />

<context:component-scan base-package="com.zgy" />

<bean

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<value>classpath:jdbc.properties</value>

</property>

</bean>

 

<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>

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<!-- 配置packagesToScan,自动扫描对应package下的文件 -->

<property name="packagesToScan">

<list>

<value>com.zgy.model</value>

</list>

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">

org.hibernate.dialect.MySQLDialect

</prop>

<prop key="hibernate.show_sql">true</prop>

</props>

</property>

</bean>

 

 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

  <property name="sessionFactory" ref="sessionFactory"></property>

 </bean>

 

 <aop:config>

  <aop:pointcut id="businessService" expression="execution(public * com.zgy.service..*.*(..))" />

  <aop:advisor pointcut-ref="businessService" advice-ref="txAdvice"/>

 </aop:config>

 

 <tx:advice id="txAdvice" transaction-manager="txManager">

  <tx:attributes>

  <tx:method name="get*" read-only="true"/>

  <tx:method name="add*" read-only="false" propagation="REQUIRED"/>

  </tx:attributes>

 </tx:advice>

</beans>



技术分享

Spring——声明式的事务处理