Atomikos多数据源配置项目实例

applicationContext.xml

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


dao层

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

实体类entity

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

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

[html] view plain copy  print?
  1. @Component  
  2. @Transactional  
  3. public class AccountManager {  
  4.     ...  
  5. }  


最后就可以通过指定不通的sessionFactory访问不同的数据库了。
上一篇:【第17个代码模型】使用 PreparedStatement 操作数据库(PrepareStatement 查询案例)|学习笔记


下一篇:如何让AI理解数学?Facebook神经网络通过“语言翻译”求解数学难题