最近研究事务说明,稍微总结一下,以后继续补充:
1.将 atomikos3.8.0 供提的相干jar包( AtomikosTransactionsEssentials-3.8.0-bin.zip),添加到项目lib目录中
atomikos-util.jar
transactions.jar
transactions-jta.jar
transactions-api.jar
transactions-hibernate3.jar
transactions-jdbc.jar
transactions-jdbc-deprecated.jar
2.修改applicationContext-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/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- 数据库息信件文 --> <property name="location"> <value>classpath:hibernate.properties</value> </property> </bean> <bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close" lazy-init="true"> <property name="uniqueResourceName"><value>OracleXADataSource</value></property> <property name="xaDataSourceClassName"><value>oracle.jdbc.xa.client.OracleXADataSource</value></property> <property name="xaProperties"> <props> <prop key="URL">${url}</prop> <prop key="user">${username}</prop> <prop key="password">${password}</prop> </props> </property> <!-- set properties for datasource connection pool --> <property name="minPoolSize"><value>2</value></property> <property name="maxPoolSize"><value>20</value></property> <property name="loginTimeout"><value>30</value></property> <property name="reapTimeout"><value>600</value></property> <!-- set a SQL for testing connection --> <property name="testQuery"><value>select 1 from dual</value></property> </bean> <bean id="dataSourceUUS" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close" lazy-init="true"> <property name="uniqueResourceName"><value>OracleXADataSourceUUS</value></property> <property name="xaDataSourceClassName"><value>oracle.jdbc.xa.client.OracleXADataSource</value></property> <property name="xaProperties"> <props> <prop key="URL">${uus_url}</prop> <prop key="user">${uus_username}</prop> <prop key="password">${uus_password}</prop> </props> </property> <!-- set properties for datasource connection pool --> <property name="minPoolSize"><value>2</value></property> <property name="maxPoolSize"><value>20</value></property> <property name="loginTimeout"><value>30</value></property> <property name="reapTimeout"><value>600</value></property> <!-- set a SQL for testing connection --> <property name="testQuery"><value>select 1 from dual</value></property> </bean> <!-- sessionFactory1--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSourceDMS"/> </property> <!-- 处置CLOB象对 --> <property name="lobHandler" ref="lobHandler" /> <property name="mappingDirectoryLocations"> <list> <value>classpath:org/hdht/</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop> <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop> <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop> <prop key="Hibernate.cache.use_query_cache">${Hibernate.cache.use_query_cache}</prop> <prop key="Hibernate.cache.query_cache_factory">${Hibernate.cache.query_cache_factory}</prop> </props> </property> </bean> <!-- sessionFactory2 --> <bean id="uusSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSourceUUS" /> </property> <!-- 处置CLOB象对 --> <property name="lobHandler" ref="lobHandler" /> <property name="mappingDirectoryLocations"> <list> <value>classpath:org/hdht/</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop> <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop> <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop> <prop key="Hibernate.cache.use_query_cache">${Hibernate.cache.use_query_cache}</prop> <prop key="Hibernate.cache.query_cache_factory">${Hibernate.cache.query_cache_factory}</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager"> <bean class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close"> <property name="forceShutdown" value="true"/> </bean> </property> <property name="userTransaction"> <bean class="com.atomikos.icatch.jta.UserTransactionImp"/> </property> </bean> <!-- ========================= ASPECT CONFIGURATION ======================== --> <aop:config proxy-target-class="true"> <!-- This definition creates auto-proxy infrastructure based on the given pointcut, expressed in AspectJ pointcut language. Here: applying the advice named "txAdvice" to all methods on classes named PetStoreImpl. --> <aop:advisor pointcut="execution(* org.hdht..*Logic.*(..))" advice-ref="txAdvice"/> </aop:config> <!-- Transaction advice definition, based on method name patterns. Defaults to PROPAGATION_REQUIRED for all methods whose name starts with "insert" or "update", and to PROPAGATION_REQUIRED with read-only hint for all other methods. --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="insert*"/> <tx:method name="save*"/> <tx:method name="add*"/> <tx:method name="create*"/> <tx:method name="update*"/> <tx:method name="modify*"/> <tx:method name="change*"/> <tx:method name="remove*"/> <tx:method name="delete*"/> <tx:method name="reset*"/> <tx:method name="recycle*"/> <tx:method name="backup*"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 处置CLOB象对 --> <bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor" lazy-init="true" /> <bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true"> <property name="nativeJdbcExtractor"> <ref local="nativeJdbcExtractor" /> </property> </bean> <!-- Use default sessionFactory MyBusinessDao1 need extends HibernateDaoSupport --> <bean id="myBusinessDao1" class="org.mycorp.business.dao.MyBusinessDao1" autowire="byName"/> <!-- Use uusSessionFactory MyBusinessDao2 need extends HibernateDaoSupport --> <bean id="myBusinessDao2" class="org.mycorp.business.dao.MyBusinessDao2" autowire="byName" > <property name="sessionFactory" ref="uusSessionFactory"/> </bean> </beans>
附1:
hibernate.properties
--------------------------------
dms_url=jdbc:oracle:thin:@192.168.1.89:1521:mas username=MAS_DMS password=MAS_DMS uus_url=jdbc:oracle:thin:@192.168.1.89:1521:mas uus_username=MAS_UUS uus_password=MAS_UUS hibernate.dialect=org.hibernate.dialect.Oracle9Dialect hibernate.show_sql=true hibernate.format_sql=false hibernate.jdbc.batch_size=50 hibernate.cache.use_second_level_cache=true hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider Hibernate.cache.use_query_cache=false Hibernate.cache.query_cache_factory=org.hibernate.cache.StandardQueryCacheFactory
附2:
tx:method 属性说明
---------------------------------
name
数参说明:与事务属性关联的方法名。通配符(*)可以用来指定一批关联到同相的事务属性的方法。如:'get*'、'handle*'、'on*Event'等等。
是不是必须:必须 默许值:无
propagation
数参说明:事务传播行为
是不是必须:不必须 默许值:REQUIRED
isolation
数参说明:事务离隔级别
是不是必须:不必须 默许值:DEFAULT
timeout
数参说明:事务时超的间时(以秒为单位)
是不是必须:不必须 默许值:-1
read-only
数参说明:事务是不是只读
是不是必须:不必须 默许值:false
rollback-for
数参说明:将被触发停止回滚的 Exception(s);以逗号离开。 如:'com.foo.MyBusinessException,ServletException'
是不是必须:不必须 默许值:无
no-rollback-for
数参说明:不被触发停止回滚的 Exception(s);以逗号离开。 如:'com.foo.MyBusinessException,ServletException'
是不是必须:不必须 默许值:无
事务的会合传播忒性
-------------------------------
1、PROPAGATION_REQUIRED:如果存在一个事务,则持支前当事务。如果没有事务则开启。
2、PROPAGATION_SUPPORTS:如果存在一个事务,持支前当事务。如果没有事务,则非事务的执行。
3、PROPAGATION_MANDATORY:如果已存在一个事务,持支前当事务。如果没有一个活动的事务,则抛出异常。
4、PROPAGATION_REQUIRES_NEW:老是开启一个新的事务。如果一个事务存在,则将这个存在的事务起挂。
5、PROPAGATION_NOT_SUPPORTED:总非是事务地执行,并起挂任何存在的事务。
6、PROPAGATION_NEVER:总非是事务地执行,如果存在一个活动事务,则抛出异常。
7、PROPAGATION_NESTED:如果一个活动的事务存在,则运行在一个套嵌的事务中,如果没有活动事务,则按TransactionDefinition.PROPAGATION_REQUIRED属性执行
Spring事务的离隔级别
-------------------------------
1、ISOLATION_DEFAULT: 这是一个PlatfromTransactionManager默许的离隔级别,应用数据库默许的事务离隔级别。
2、ISOLATION_READ_UNCOMMITTED:这是事务最低的离隔级别,它答应另外一个事务可以看到这个事务未交提的数据。
3、ISOLATION_READ_COMMITTED:保障一个事务修改的数据交提后才能被另外一个事务读取。另外一个事务不能读取该事务未交提的数据。
4、ISOLATION_REPEATALBE_READ: 种这事务离隔级别可以止防读脏,不可重复读。但是可能涌现幻想读。它除了保障一个事务不能读取另外一个事务未交提的数据外,还保障了免避面下的况情发生(不可重复读)。
5、ISOLATION_SERIALIZABLE 这是费花最高价值但是最靠可的事务离隔级别。事务被处置为序顺执行。除了止防读脏,不课重复读外,还免避了幻想读。
文章结束给大家分享下程序员的一些笑话语录: 《诺基亚投资手机浏览器UCWEB,资金不详或控股》杯具了,好不容易养大的闺女嫁外国。(心疼是你养的吗?中国创业型公司创业初期哪个从国有银行贷到过钱?)