事物概念:是由一步或几步基本操作组成的逻辑执行单元。 事物的特性:原子性,一致性,隔离型,持久性。 对事物管理可以采用两个策略: 全局事物:全局事物通常由应用服务器管理,使用JTA.全局事物可跨越对个事物性的资源,保证在多个事物性资源间跨越时资源的一致性。 局部事物:局部事物和特定资源相关,例如,一个和jdbc连接关联的事物。该事物仅能保证对该jdbc连接数据库的一致性。对局部事物,应用服务器不需要参与事物管理,不能保证跨越多个资源的事物的正确性。 Hibernate局部事物: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans default-lazy-init="false"> <!-- ========================= Start of PERSISTENCE DEFINITIONS ========================= --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:init.properties</value> </property> </bean> <!--定义数据源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" dependency-check="none"> <property name="driverClass"> <value>${datasource.driverClassName}</value> </property> <property name="jdbcUrl"> <value>${datasource.url}</value> </property> <property name="user"> <value>${datasource.username}</value> </property> <property name="password"> <value>${datasource.password}</value> </property> <property name="minPoolSize"> <value>${c3p0.minPoolSize}</value> </property> <property name="maxPoolSize"> <value>${c3p0.maxPoolSize}</value> </property> <property name="initialPoolSize"> <value>${c3p0.initialPoolSize}</value> </property> <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime"> <value>${c3p0.maxIdleTime}</value> </property> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement"> <value>${c3p0.acquireIncrement}</value> </property> <property name="maxStatements"> <value>${c3p0.maxStatements}</value> </property> <property name="idleConnectionTestPeriod"> <value>${c3p0.idleConnectionTestPeriod}</value> </property> <property name="numHelperThreads"> <value>${c3p0.numHelperThreads}</value> </property> </bean> <!--定义hibernate的sessionFactory--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!--依赖注入数据源,注入正是上文定义的dataSource--> <property name="dataSource"> <ref local="dataSource" /> </property>
<property name="mappingResources"> <!--以下用来列出所有的po映射文件--> <list> <value>com/cader/bean/TEquipContractTypedict.hbm.xml</value> </list> </property> <!--定义hibernate的sessionFactory属性--> <property name="hibernateProperties"> <props> <!--定义hibernate的连接方言--> <prop key="hibernate.dialect"> ${hibernate.dialect} </prop> <!--定义hibernate的sql语句是否显示--> <prop key="hibernate.show_sql"> ${hibernate.show_sql} </prop> <!--定义hibernate对数据库进行批量删除,批量更新和批量插入的时候的批次大小--> <prop key="hibernate.jdbc.batch_size"> ${hibernate.jdbc.batch_size} </prop> <!--定义hibernate每次从数据库中取出的记录条数--> <prop key="hibernate.jdbc.fetch_size"> ${hibernate.jdbc.fetch_size} </prop> <prop key="hibernate.query.factory_class"> ${hibernate.query.factory_class} </prop> </props> </property> </bean> <!--配置hibernate的事物管理--> <!--使用HibernateTransactionManager类,该类是PlatformTransactionManger接口针对采用Hibernate持久化连接的特定实现--> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <!--HibernateTransactionManager bean需要依赖注入一个sessionFactory bean的应用--> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> </beans> Hibernate全局事物 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans >
<!--配置jndi数据源--> <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <!--容器管理数据源的jndi--> <property name="jndiName"> <value>jdbc/jpetstore</value> </property>
</bean> <!--定义hibernate的sessionFactory--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!--依赖注入数据源,注入正是上文定义的dataSource--> <property name="dataSource"> <ref local="dataSource" /> </property>
<property name="mappingResources"> <!--以下用来列出所有的po映射文件--> <list> <value>com/cader/bean/TEquipContractTypedict.hbm.xml</value> </list> </property> <!--定义hibernate的sessionFactory属性--> <property name="hibernateProperties"> <props> <!--定义hibernate的连接方言--> <prop key="hibernate.dialect"> ${hibernate.dialect} </prop> <!--定义hibernate的sql语句是否显示--> <prop key="hibernate.show_sql"> ${hibernate.show_sql} </prop> <!--定义hibernate对数据库进行批量删除,批量更新和批量插入的时候的批次大小--> <prop key="hibernate.jdbc.batch_size"> ${hibernate.jdbc.batch_size} </prop> <!--定义hibernate每次从数据库中取出的记录条数--> <prop key="hibernate.jdbc.fetch_size"> ${hibernate.jdbc.fetch_size} </prop> <prop key="hibernate.query.factory_class"> ${hibernate.query.factory_class} </prop> </props> </property> </bean> <!--使用JtaTransactionManager类,该类是PlatformTransactionManger接口针对采用数据源连接的特定实现--> <!---JtaTransactionManager不需要知道数据源,或任何其他特定资源 因为使用容器的全局事物管理--> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
</bean>
</beans> 根据bean Name 自动创建事物代理: <!--定义事物拦截器--> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <!-- 事务拦截器bean需要依赖注入一个事务管理器 --> <property name="transactionManager" ref="transactionManager"/> <property name="transactionAttributes"> <!-- 下面定义事务传播属性--> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean>
<!-- 定义BeanNameAutoProxyCreator--> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <!-- 指定对满足哪些bean name的bean自动生成业务代理 --> <property name="beanNames"> <!-- 下面是所有需要自动创建事务代理的bean--> <list> <value>test1</value> <value>test2</value> </list> <!-- 此处可增加其他需要自动创建事务代理的bean--> </property> (2)<!-- 指定对满足哪些bean name的bean自动生成业务代理 --> <property name="beanNames"> <value>*DAO,*.Service</value> </property> <!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器--> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> <!-- 此处可增加其他新的Interceptor --> </list> </property> </bean> 使用bean继承简化事物代理的配置 <bean id="test1" class="lee.TransactionTestImpl"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!--定义所有事物代理bean模板--> <bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <property name="transactionAttributes"> <props> <prop key="insert*"> PROPAGATION_REQUIRED,-com.cader.web.util.ChisException </prop> <prop key="add*"> PROPAGATION_REQUIRED,-com.cader.web.util.ChisException </prop> <prop key="save*"> PROPAGATION_REQUIRED,-com.cader.web.util.ChisException </prop>
</props> </property> </bean> <!--让事物代理bean继承模板bean--> <bean id="testTrans1" parent="txProxyTemplate"> <property name="target"> <ref local="test1"/> </property> </bean> |