当前位置:   article > 正文

hibernate+atomikos分布式事务配置_hibernate atomikos

hibernate atomikos
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  8. http://www.springframework.org/schema/tx
  9. http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  14. ">
  15. <context:property-placeholder location="classpath:config.properties" />
  16. <!-- 开启编程式事务 -->
  17. <tx:annotation-driven transaction-manager="springTransactionManager" />
  18. <!-- 用atomikosDataSourceBean配置数据源1 -->
  19. <bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean">
  20. <property name="uniqueResourceName">
  21. <value>db1</value>
  22. </property>
  23. <property name="xaDataSourceClassName">
  24. <value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>
  25. </property>
  26. <property name="xaProperties">
  27. <props>
  28. <prop key="user">root</prop>
  29. <prop key="password">root</prop>
  30. <prop key="url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
  31. </prop>
  32. </props>
  33. </property>
  34. <property name="maxPoolSize">
  35. <value>3</value>
  36. </property>
  37. </bean>
  38. <!-- session工厂 -->
  39. <bean id="sessionFactory"
  40. class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  41. <property name="dataSource" ref="dataSource" />
  42. <property name="hibernateProperties">
  43. <props>
  44. <prop key="hibernate.hbm2ddl.auto">update</prop>
  45. <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  46. <prop key="hibernate.show_sql">true</prop>
  47. <prop key="hibernate.format_sql">true</prop>
  48. </props>
  49. </property>
  50. <!-- 注解方式配置 -->
  51. <property name="packagesToScan">
  52. <list>
  53. <value>com.feng.model</value>
  54. </list>
  55. </property>
  56. </bean>
  57. <bean id="dataSource2" class="com.atomikos.jdbc.AtomikosDataSourceBean">
  58. <property name="uniqueResourceName">
  59. <value>db2</value>
  60. </property>
  61. <property name="xaDataSourceClassName">
  62. <value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>
  63. </property>
  64. <property name="xaProperties">
  65. <props>
  66. <prop key="user">root</prop>
  67. <prop key="password">root</prop>
  68. <prop key="url">jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=UTF-8
  69. </prop>
  70. </props>
  71. </property>
  72. <property name="maxPoolSize">
  73. <value>3</value>
  74. </property>
  75. </bean>
  76. <!-- session工厂 -->
  77. <bean id="sessionFactory2"
  78. class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  79. <property name="dataSource" ref="dataSource2" />
  80. <property name="hibernateProperties">
  81. <props>
  82. <prop key="hibernate.hbm2ddl.auto">update</prop>
  83. <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  84. <prop key="hibernate.show_sql">true</prop>
  85. <prop key="hibernate.format_sql">true</prop>
  86. </props>
  87. </property>
  88. <!-- 注解方式配置 -->
  89. <property name="packagesToScan">
  90. <list>
  91. <value>com.feng.model2</value>
  92. </list>
  93. </property>
  94. </bean>
  95. <!-- 配置atomikos事务管理 -->
  96. <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
  97. init-method="init" destroy-method="close">
  98. <property name="forceShutdown">
  99. <value>true</value>
  100. </property>
  101. </bean>
  102. <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
  103. <property name="transactionTimeout">
  104. <value>30000</value>
  105. </property>
  106. </bean>
  107. <!-- 用jta来做事务管理器 -->
  108. <bean id="springTransactionManager"
  109. class="org.springframework.transaction.jta.JtaTransactionManager">
  110. <property name="transactionManager" ref="atomikosTransactionManager" />
  111. <property name="userTransaction" ref="atomikosUserTransaction" />
  112. </bean>
  113. <tx:advice id="txAdvice" transaction-manager="springTransactionManager">
  114. <tx:attributes>
  115. <!-- 针对不同的方法的具体事务操作 -->
  116. <tx:method name="add*" propagation="REQUIRED" />
  117. <tx:method name="save*" propagation="REQUIRED" />
  118. <!-- read-only="true" 表示只读 -->
  119. <tx:method name="*" propagation="REQUIRED" read-only="true" />
  120. </tx:attributes>
  121. </tx:advice>
  122. <aop:config>
  123. <aop:pointcut expression="execution(* com.feng.service.*.*(..))" id="serviceOperation"/>
  124. <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
  125. </aop:config>
  126. </beans>



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/346902
推荐阅读
  

闽ICP备14008679号