Spring事务管理

来源:互联网 发布:java重载函数是什么 编辑:程序博客网 时间:2024/06/08 00:27

       Aop的使用使得Spring事务管理更加具有灵活性,已使得事务的提交更加合理化了,Spring和Hibernate结合的时候,我们知道SessionFactory是由Spring提供的,因此,事务自然而然也应该由Spring管理。Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。

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"><bean id="jdbc"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="oracle.jdbc.OracleDriver" /><property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" /><property name="username" value="scott" /><property name="password" value="accp" /></bean><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><propkey="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop><!-- Spring默认值,相当于手动管理事务的<prop key="hibernate.current_session_context_class">thread</prop> --></props></property></bean><bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean><tx:advice id="txAdvice" transaction-manager="transactionManager"><!-- 事务管理器 --><tx:attributes><tx:method name="login" propagation="REQUIRED" /></tx:attributes></tx:advice><aop:config><aop:pointcutexpression="execution(* org.han.service.*.*(..))"id="pointcut" /><aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" /></aop:config></beans>

Spring-beans.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"xsi:schemaLocation="http://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"><import resource="Spring-Hibernate.xml" /><bean id="base" class="org.han.dao.BaseDao" abstract="true"><property name="sessionFactory" ref="sessionFactory" /></bean><bean id="userdao" class="org.han.dao.impl.UserDaoImpl" parent="base"></bean><bean id="logdao" class="org.han.dao.impl.LogDaoImpl" parent="base"></bean></beans>
上面的例子中,我sessionFactory作为了BaseDao的一个属性,所有的Dao继承BaseDao,从BaseDao抽象类中得到sessionFactory

import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;import org.hibernate.SessionFactory;public abstract class BaseDao<T>{private Class entityClass;protected SessionFactory sessionFactory;public SessionFactory getSessionFactory() {return sessionFactory;}public void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}public BaseDao() {super();// TODO Auto-generated constructor stubentityClass = getParameterizedType(this.getClass());}/* * 获得当前类的Class */private Class getParameterizedType(Class c) {Type type = c.getGenericSuperclass();ParameterizedType parametType = null;if (type instanceof ParameterizedType) {parametType = (ParameterizedType) type;} else {try {throw new Exception("not find ParameterizedType!");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}Type[] types = parametType.getActualTypeArguments();return (Class) types[0];}/* * 通用通过ID获得实体类的方法 */protected T getEntityById(Integer primaryKey) {T t = (T) this.sessionFactory.getCurrentSession().get(this.entityClass, primaryKey);return t;}/* * 公用的save方法 */public int saveObject(Object obj){return (Integer)this.sessionFactory.getCurrentSession().save(obj);}}



原创粉丝点击