手动模拟Spring管理事务

来源:互联网 发布:linux设置命令行启动 编辑:程序博客网 时间:2024/05/19 02:04
Spring-Hibernate.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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><!-- 使用c3p0作为数据源 --><bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close"><property name="driverClass" value="oracle.jdbc.OracleDriver" /><property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl" /><property name="user" value="scott" /><property name="password" value="accp" /><!-- 指定连接数据库连接池的最大连接数 --><property name="maxPoolSize" value="20" /><!-- 指定连接数据库连接池的最小连接数 --><property name="minPoolSize" value="1" /><!-- 指定连接数据库连接池的初始化连接数 --><property name="initialPoolSize" value="5" /><!-- 指定连接数据库连接池的连接的最大空闲时间 --><property name="maxIdleTime" value="20" /></bean><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" scope="singleton"><property name="dataSource" ref="datasource"></property><property name="mappingResources"><list><value>org/han/entity/Log.hbm.xml</value><value>org/han/entity/User.hbm.xml</value></list></property><property name="hibernateProperties"><props><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop><prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop><prop key="hibernate.current_session_context_class">thread</prop></props></property></bean></beans>


创建Aop拦截器:

import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;import org.hibernate.SessionFactory;public class LogInterceptor implements MethodInterceptor {private SessionFactory sessionFactory;public SessionFactory getSessionFactory() {return sessionFactory;}public void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}@Overridepublic Object invoke(MethodInvocation arg0) throws Throwable {// TODO Auto-generated method stubObject obj=null;try{sessionFactory.getCurrentSession().beginTransaction();obj = arg0.proceed();sessionFactory.getCurrentSession().getTransaction().commit();}catch(Exception e){sessionFactory.getCurrentSession().getTransaction().rollback();}return obj;}}


配置xml:

<bean id="loginter" class="org.han.aop.LogInterceptor"><property name="sessionFactory" ref="sessionFactory" /></bean><aop:config><aop:pointcut expression="execution(* org.han.service.impl.*.*(..))" id="logpoint"/><aop:advisor advice-ref="loginter" pointcut-ref="logpoint"/></aop:config>

使用AOP后就更充分的保证了数据的完整性,做到这里就差不多了,只需要对不同业务逻辑做事务的拦截。