当前位置:   article > 正文

Atomikos多数据源配置项目实例_atomikosdatasourebean 'tifarecorddatasource': pool

atomikosdatasourebean 'tifarecorddatasource': poolsize equals default - this

applicationContext.xml

  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:jee="http://www.springframework.org/schema/jee"
  4. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/tx
  8. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  9. http://www.springframework.org/schema/jee
  10. http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context-3.0.xsd"
  13. default-lazy-init="true">
  14. <description>Spring公共配置 </description>
  15. <!-- 定义受环境影响易变的变量 -->
  16. <bean
  17. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  18. <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
  19. <property name="ignoreResourceNotFound" value="true" />
  20. <property name="locations">
  21. <list>
  22. <!-- 标准配置 -->
  23. <value>classpath*:/application.properties</value>
  24. <!--
  25. 集群中节点配置 <value>classpath*:/application.cluster.properties</value>
  26. -->
  27. <!--
  28. 本地开发环境配置 <value>classpath*:/application.local.properties</value>
  29. -->
  30. <!--
  31. 服务器生产环境配置 <value>/pom/application.server.properties</value>
  32. -->
  33. </list>
  34. </property>
  35. </bean>
  36. <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
  37. <context:component-scan base-package="com.wiseweb.pom" />
  38. <!-- orable配置 -->
  39. <!-- <bean id="dataSourceContent" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
  40. <property name="uniqueResourceName">
  41. <value>jdbc/dataSourceContent</value>
  42. </property>
  43. <property name="xaDataSourceClassName">
  44. <value>oracle.jdbc.xa.client.OracleXADataSource</value>
  45. </property>
  46. <property name="xaProperties">
  47. <props>
  48. <prop key="URL">${jdbc.url}</prop>
  49. <prop key="user">${jdbc.username}</prop>
  50. <prop key="password">${jdbc.password}</prop>
  51. </props>
  52. </property>
  53. <property name="poolSize">
  54. <value>5</value>
  55. </property>
  56. <property name="maxPoolSize">
  57. <value>15</value>
  58. </property>
  59. </bean>
  60. <bean id="dataSourceIndex" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
  61. <property name="uniqueResourceName">
  62. <value>jdbc/dataSourceIndex</value>
  63. </property>
  64. <property name="xaDataSourceClassName">
  65. <value>oracle.jdbc.xa.client.OracleXADataSource</value>
  66. </property>
  67. <property name="xaProperties">
  68. <props>
  69. <prop key="URL">${jdbc.url}</prop>
  70. <prop key="user">${jdbc.username}</prop>
  71. <prop key="password">${jdbc.password}</prop>
  72. </props>
  73. </property>
  74. <property name="poolSize">
  75. <value>5</value>
  76. </property>
  77. <property name="maxPoolSize">
  78. <value>15</value>
  79. </property>
  80. </bean> -->
  81. <!-- 本地数据库配置 -->
  82. <bean id="dataSourceContent" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
  83. <property name="uniqueResourceName">
  84. <value>jdbc/dataSourceContent</value>
  85. </property>
  86. <property name="xaDataSourceClassName">
  87. <value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>
  88. </property>
  89. <property name="xaProperties">
  90. <props>
  91. <prop key="serverName">${jdbc.servername}</prop>
  92. <prop key="portNumber">${jdbc.portnumber}</prop>
  93. <prop key="databaseName">${jdbc.databasename}</prop>
  94. <prop key="user">${jdbc.username}</prop>
  95. <prop key="password">${jdbc.password}</prop>
  96. </props>
  97. </property>
  98. <property name="poolSize">
  99. <value>5</value>
  100. </property>
  101. <property name="maxPoolSize">
  102. <value>15</value>
  103. </property>
  104. <property name="testQuery">
  105. <value>select 1</value>
  106. </property>
  107. </bean>
  108. <bean id="dataSourceIndex" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
  109. <property name="uniqueResourceName">
  110. <value>jdbc/dataSourceIndex</value>
  111. </property>
  112. <property name="xaDataSourceClassName">
  113. <value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>
  114. </property>
  115. <property name="xaProperties">
  116. <props>
  117. <prop key="serverName">${jdbc.servername_index}</prop>
  118. <prop key="portNumber">${jdbc.portnumber_index}</prop>
  119. <prop key="databaseName">${jdbc.databasename_index}</prop>
  120. <prop key="user">${jdbc.username_index}</prop>
  121. <prop key="password">${jdbc.password_index}</prop>
  122. </props>
  123. </property>
  124. <property name="poolSize">
  125. <value>5</value>
  126. </property>
  127. <property name="maxPoolSize">
  128. <value>15</value>
  129. </property>
  130. <property name="testQuery">
  131. <value>select 1</value>
  132. </property>
  133. </bean>
  134. <!-- 数据源配置,使用应用内的DBCP数据库连接池
  135. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  136. destroy-method="close">
  137. Connection Info
  138. <property name="driverClassName" value="${jdbc.driver}" />
  139. <property name="url" value="${jdbc.url}" />
  140. <property name="username" value="${jdbc.username}" />
  141. <property name="password" value="${jdbc.password}" />
  142. Connection Pooling Info
  143. <property name="maxIdle" value="${dbcp.maxIdle}" />
  144. <property name="maxActive" value="${dbcp.maxActive}" />
  145. <property name="defaultAutoCommit" value="false" />
  146. <property name="timeBetweenEvictionRunsMillis" value="3600000" />
  147. <property name="minEvictableIdleTimeMillis" value="3600000" />
  148. </bean>
  149. --><!-- 数据源配置,使用应用服务器的数据库连接池 -->
  150. <!--
  151. <jee:jndi-lookup id="dataSource"
  152. jndi-name="java:comp/env/jdbc/ExampleDB" />
  153. -->
  154. <!-- Hibernate配置
  155. <bean id="sessionFactory"
  156. class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  157. <property name="dataSource" ref="dataSource" />
  158. <property name="namingStrategy">
  159. <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
  160. </property>
  161. <property name="hibernateProperties">
  162. <props>
  163. <prop key="hibernate.dialect">${hibernate.dialect}</prop>
  164. <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
  165. <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
  166. <prop key="hibernate.cache.use_second_level_cache">${hibernate.use_second_level_cache}</prop>
  167. <prop key="hibernate.cache.use_query_cache">${hibernate.use_query_cache}</prop>
  168. <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
  169. <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache/ehcache-hibernate-local.xml</prop>
  170. </props>
  171. </property>
  172. <property name="packagesToScan" value="com.wiseweb.pom.entity" />
  173. </bean>
  174. -->
  175. <!-- Hibernate配置 -->
  176. <!-- 本地数据库Hibernate配置 -->
  177. <bean id="sessionFactoryContent" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  178. <property name="dataSource" ref="dataSourceContent" />
  179. <property name="namingStrategy">
  180. <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
  181. </property>
  182. <property name="hibernateProperties">
  183. <props>
  184. <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
  185. <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
  186. <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
  187. <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
  188. <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache/ehcache-hibernate-local.xml</prop>
  189. </props>
  190. </property>
  191. <property name="packagesToScan" value="com.wiseweb.pom.entity" />
  192. </bean>
  193. <!-- 远程数据库Hibernate配置 -->
  194. <bean id="sessionFactoryIndex" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  195. <property name="dataSource" ref="dataSourceIndex" />
  196. <property name="namingStrategy">
  197. <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
  198. </property>
  199. <property name="hibernateProperties">
  200. <props>
  201. <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
  202. <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
  203. <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
  204. <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
  205. <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache/ehcache-hibernate-local.xml</prop>
  206. </props>
  207. </property>
  208. <property name="packagesToScan" value="com.wiseweb.pom.entity" />
  209. </bean>
  210. <!-- 事务管理器配置,单数据源事务
  211. <bean id="transactionManager"
  212. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  213. <property name="sessionFactory" ref="sessionFactory" />
  214. </bean>
  215. -->
  216. <!-- ActiveMQ JMS服务 -->
  217. <!--创建连接工厂-->
  218. <bean id="connectionFactory"
  219. class="org.apache.activemq.ActiveMQConnectionFactory">
  220. <property name="brokerURL" value="tcp://127.0.0.1:61616"></property>
  221. </bean>
  222. <!-- 声明ActiveMQ队列消息目标-->
  223. <!-- <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
  224. <constructor-arg index="0" value="wiseweb.queue"></constructor-arg>
  225. </bean> -->
  226. <!-- 声明ActiveMQ主题消息目标 -->
  227. <bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">
  228. <constructor-arg index="0" value="wiseweb.topic"></constructor-arg>
  229. </bean>
  230. <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
  231. <property name="connectionFactory" ref="connectionFactory"></property>
  232. <property name="defaultDestination" ref="destination"></property>
  233. <property name="receiveTimeout" value="10000"></property>
  234. </bean>
  235. <!-- 消息监听适配器 -->
  236. <bean id="messageListenerAdapter" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
  237. <property name="delegate">
  238. <bean class="com.wiseweb.pom.jms.listener.MessageListener"></bean>
  239. </property>
  240. <property name="defaultListenerMethod" value="receiveMessage"></property>
  241. <property name="defaultResponseDestination" ref="destination"></property>
  242. </bean>
  243. <!-- 监听容器 -->
  244. <!-- <bean id="messageListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
  245. <property name="connectionFactory" ref="connectionFactory"></property>
  246. <property name="destination" ref="destination"></property>
  247. <property name="messageListener" ref="messageListenerAdapter"></property>
  248. </bean> -->
  249. <bean id="messageSender" class="com.wiseweb.pom.jms.MessageSender">
  250. <property name="jmsTemplate" ref="jmsTemplate"></property>
  251. </bean>
  252. <!-- 事务管理器配置,多数据源JTA事务-->
  253. <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
  254. <property name="forceShutdown"><value>true</value></property>
  255. </bean>
  256. <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
  257. <property name="transactionTimeout" value="300"/>
  258. </bean>
  259. <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
  260. <property name="transactionManager" ref="atomikosTransactionManager" />
  261. <property name="userTransaction" ref="atomikosUserTransaction"/>
  262. <property name="allowCustomIsolationLevels" value="true"/>
  263. </bean>
  264. <!-- 使用annotation定义事务 -->
  265. <tx:annotation-driven transaction-manager="transactionManager"
  266. proxy-target-class="true" />
  267. <!-- 加载网站类型 -->
  268. <bean id="groupMap" class="java.util.TreeMap">
  269. <constructor-arg>
  270. <map>
  271. <entry key="1" value="新闻" />
  272. <entry key="2" value="论坛" />
  273. <entry key="3" value="博客" />
  274. <entry key="4" value="微博" />
  275. <entry key="5" value="纸媒" />
  276. <!-- <entry key="6" value="视频" />-->
  277. <entry key="7" value="外媒" />
  278. <!-- <entry key="8" value="广播" />
  279. <entry key="9" value="电视" />
  280. <entry key="10" value="空间" />
  281. <entry key="11" value="微信" />-->
  282. </map>
  283. </constructor-arg>
  284. </bean>
  285. <bean id="staticValue" class="com.wiseweb.util.StaticValue"
  286. lazy-init="false">
  287. <property name="groupMap">
  288. <ref bean="groupMap" />
  289. </property>
  290. </bean>
  291. <bean id="hibernateDaoImple" class="com.wiseweb.pom.dao.HibernateGeneralDao">
  292. <property name="sessionFactory" ref="sessionFactoryContent"></property>
  293. </bean>
  294. <!-- 配置定时器 -->
  295. <bean id="scheduler"
  296. class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  297. </bean>
  298. <bean id="quartzTimerUtil" class="com.wiseweb.util.sms.QuartzTimerUtil">
  299. <property name="scheduler" ref="scheduler" />
  300. </bean>
  301. <bean id="startAllTimer" class="com.wiseweb.util.sms.StartAllTimer">
  302. <property name="scheduler" ref="scheduler" />
  303. <property name="hibernateDaoImple" ref="hibernateDaoImple"></property>
  304. </bean>
  305. <!-- 配置定时器结束 -->
  306. </beans>


dao层

  1. package com.wiseweb.pom.dao.account;
  2. import javax.annotation.Resource;
  3. import org.hibernate.SessionFactory;
  4. import org.springframework.stereotype.Component;
  5. import org.springside.modules.orm.hibernate.HibernateDao;
  6. import com.wiseweb.pom.entity.Company;
  7. /**
  8. * HibernateDao中的Company是实体类
  9. * @author Benjamin
  10. *
  11. */
  12. @Component
  13. public class CompanyDao extends HibernateDao<Company, Long>
  14. {
  15. //选择applicationContext中名为sessionFactoryContent的sessionFactory注入,实现多数据源访问
  16. @Override
  17. @Resource(name = "sessionFactoryContent")
  18. public void setSessionFactory(SessionFactory sessionFactory) {
  19. // TODO Auto-generated method stub
  20. super.setSessionFactory(sessionFactory);
  21. }
  22. }

实体类entity

  1. package com.wiseweb.pom.entity;
  2. import java.io.Serializable;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import javax.persistence.CascadeType;
  6. import javax.persistence.Column;
  7. import javax.persistence.Entity;
  8. import javax.persistence.Id;
  9. import javax.persistence.OneToMany;
  10. import javax.persistence.Table;
  11. import javax.persistence.Transient;
  12. @Entity
  13. @Table(name="wise_company")
  14. public class Company implements Serializable{
  15. /**
  16. *
  17. */
  18. private Long id ;
  19. private static final long serialVersionUID = 1L;
  20. private String name ;
  21. private Integer grade ;
  22. private Long parentId ;
  23. private Integer nodeType ;
  24. private Integer orderType ;
  25. private Integer flag ;
  26. private Company fCompany ;
  27. private List<Department> departments = new ArrayList<Department>() ;
  28. private Long companyNum ;
  29. private Long childNum ;
  30. private List<Company> childCompanys = new ArrayList<Company>() ;
  31. @Column(name="name", nullable=false, length=45)
  32. public String getName() {
  33. return name;
  34. }
  35. public void setName(String name) {
  36. this.name = name;
  37. }
  38. @Column(name="grade", nullable=false)
  39. public Integer getGrade() {
  40. return grade;
  41. }
  42. public void setGrade(Integer grade) {
  43. this.grade = grade;
  44. }
  45. @Column(name="parent_id", nullable=false)
  46. public Long getParentId() {
  47. return parentId;
  48. }
  49. public void setParentId(Long parentId) {
  50. this.parentId = parentId;
  51. }
  52. @Column(name="node_type", nullable=false)
  53. public Integer getNodeType() {
  54. return nodeType;
  55. }
  56. public void setNodeType(Integer nodeType) {
  57. this.nodeType = nodeType;
  58. }
  59. @Column(name="order_type")
  60. public Integer getOrderType() {
  61. return orderType;
  62. }
  63. public void setOrderType(Integer orderType) {
  64. this.orderType = orderType;
  65. }
  66. @Column(name="flag", nullable=false)
  67. public Integer getFlag() {
  68. return flag;
  69. }
  70. public void setFlag(Integer flag) {
  71. this.flag = flag;
  72. }
  73. @OneToMany(mappedBy="company",cascade = {CascadeType.ALL})
  74. public List<Department> getDepartments() {
  75. return departments;
  76. }
  77. public void setDepartments(List<Department> departments) {
  78. this.departments = departments;
  79. }
  80. @Transient
  81. public Company getFCompany() {
  82. return fCompany;
  83. }
  84. public void setFCompany(Company fCompany) {
  85. this.fCompany = fCompany;
  86. }
  87. @Id
  88. public Long getId() {
  89. return id;
  90. }
  91. public void setId(Long id) {
  92. this.id = id;
  93. }
  94. @Transient
  95. public Long getCompanyNum() {
  96. return companyNum;
  97. }
  98. public void setCompanyNum(Long companyNum) {
  99. this.companyNum = companyNum;
  100. }
  101. @Transient
  102. public List<Company> getChildCompanys() {
  103. return childCompanys;
  104. }
  105. public void setChildCompanys(List<Company> childCompanys) {
  106. this.childCompanys = childCompanys;
  107. }
  108. @Transient
  109. public Long getChildNum() {
  110. return childNum;
  111. }
  112. public void setChildNum(Long childNum) {
  113. this.childNum = childNum;
  114. }
  115. }

下面最重要的是service层,必须要加上@Transactional不然会报the XA resource is currently involved in a local (non-XA) transaction的错误。

  1. @Component
  2. @Transactional
  3. public class AccountManager {
  4. ...
  5. }


最后就可以通过指定不通的sessionFactory访问不同的数据库了。

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/346891
推荐阅读
相关标签
  

闽ICP备14008679号