工作总结(鄂州商城数据库配置)————spring配置多数据源+atomikos分布式事务

         宜宾商城用的是druid数据库,鄂州商城用的是多数据源。于是想总结这块的数据库配置。先看鄂州的。

   ---------------------背景-------------

        鄂州商城有一部分信息,是深圳项目组做的后台系统发布的数据,我需要展示在门户网站上,可以让同事写接口提供。但是我直接配数据库直接读取了(多数据源)。

        其实简单配个多数据源我这就够用了,因为本身没有分布式事务的业务。 之前的招标采购系统 有报建,历史,正式多个库,且业务上多库间需要维护事务,那里才用的合适。当然鄂州这里我们还是实现分布式事务。  

---------大致讲下鄂州的数据库配置------

1.依赖包  pom.xml 中

<!-- Atomikos dependencies -->
<dependency>
   <groupId>com.atomikos</groupId>
   <artifactId>transactions</artifactId>
   <version>3.9.3</version>
</dependency>
<dependency>
   <groupId>com.atomikos</groupId>
   <artifactId>transactions-jta</artifactId>
   <version>3.9.3</version>
</dependency>
<dependency>
   <groupId>com.atomikos</groupId>
   <artifactId>transactions-jdbc</artifactId>
   <version>3.9.3</version>
</dependency>
2.配置数据源

<!-- 交易网的数据库 -->
<bean id="dataSourceJyw" class="com.atomikos.jdbc.AtomikosDataSourceBean">
   <property name="uniqueResourceName" value="dataSourceJyw"/>
   <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
   <property name="xaProperties">
      <props>
         <prop key="user">${userjyw}</prop>       
         <prop key="password">${passwordjyw}</prop>       
         <prop key="URL">${urljyw}</prop>   
         <prop key="pinGlobalTxToPhysicalConnection">true</prop>       
      </props>
   </property>
   <property name="minPoolSize" value="2"/>
   <property name="maxPoolSize" value="${cpool.maxPoolSize}"/>
   <property name="maxIdleTime" value="${cpool.maxIdleTime}"/>
   <property name="testQuery" value="select 1 "/>
</bean>

3.使用数据源 ,配置sessionfactory

<bean id="sessionFactoryJyw" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
   <property name="dataSource" ref="dataSourceJyw" />
   <property name="jtaTransactionManager" ref="atomikosTransactionManager" /> 
   <property name="hibernateProperties">
      <value>
      hibernate.dialect=org.hibernate.dialect.MySQLDialect
      hibernate.show_sql=true
      hibernate.format_sql=true
      hibernate.query.substitutions=true 1, false 0
      hibernate.jdbc.batch_size=20
      hibernate.current_session_context_class=jta
      hibernate.transaction.factory_class=org.hibernate.transaction.JTATransactionFactory
      <!-- hibernate.connection.release_mode=after_statement -->
      </value>
   </property>
   <property name="packagesToScan">
      <list>  
         <value>com.truelore.jyw.*</value>  
      </list>  
   </property>  
</bean>

4.配置事务

<!-- 配置事务 -->
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
   <property name="forceShutdown" value="true"/>
   <!-- <property name="startupTransactionService" value="true" />
       <property name="transactionTimeout" value="300" /> -->
</bean>

<!-- Atomikos事务实现 -->
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
   <property name="transactionTimeout" value="300"/>
</bean>

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
   <property name="transactionManager" ref="atomikosTransactionManager"/>
   <property name="userTransaction" ref="atomikosUserTransaction"/>
   <!-- <property name="allowCustomIsolationLevels" value="true"/> -->
</bean>
<tx:annotation-driven transaction-manager="transactionManager" order="1" />
5.atomikos的配置文件jta.properties  




----------------------附-----------------------

 直接贴上spring-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:jee="http://www.springframework.org/schema/jee"
   xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
                       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
                       http://www.springframework.org/schema/jee 
                       http://www.springframework.org/schema/jee/spring-jee-4.0.xsd 
                       http://www.springframework.org/schema/context 
                       http://www.springframework.org/schema/context/spring-context-4.0.xsd
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
   <!-- 消息处理 -->                                              
   <bean id="messageSource"  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
     <property name="cacheSeconds" value="-1"/>
     <property name="basenames">
      <list>
         <value>classpath:i18n/messages</value>
         <value>classpath:com/truelore/security/messages</value>
         <value>classpath*:com/truelore/security/messages</value>
      </list>
     </property>
   </bean>


   <!--自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面 --> 
    <aop:aspectj-autoproxy /> 
   
    <!-- 使用annotation自动注入bean,并启动相关处理注解的进程 -->
   <context:component-scan base-package="com.truelore">
      <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />  
      <context:exclude-filter type="regex" expression="com.truelore.xunjia.dwr.*"/>
      <context:exclude-filter type="regex" expression="com.truelore.xunjia.activemq.*"/>
   </context:component-scan>                         
   
   <!-- 手动注入的bean -->
   <bean id="httpClientUtils" class="com.truelore.wssc.thirdmall.util.HttpClientUtils"></bean>

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations">
         <list>
            <value>classpath:custom.properties</value>
            <value>classpath:default.properties</value>
            <value>classpath:url.properties</value>
            <value>classpath:jdbc.properties</value>
            <value>classpath:jta.properties</value>
            <value>classpath:redis.properties</value>
         </list>
      </property>
   </bean>
                
   <bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean">
      <property name="uniqueResourceName" value="dataSource"/>
      <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
      <property name="xaProperties">
         <props>
            <prop key="user">${userzs}</prop>        
            <prop key="password">${passwordzs}</prop>        
            <prop key="URL">${urlzs}</prop>    
            <prop key="pinGlobalTxToPhysicalConnection">true</prop>       
         </props>
      </property>
      <property name="minPoolSize" value="2"/>
      <property name="maxPoolSize" value="${cpool.maxPoolSize}"/>
      <property name="maxIdleTime" value="${cpool.maxIdleTime}"/>
      <property name="testQuery" value="select 1 "/>
   </bean>
   
   <bean id="dataSourceZb" class="com.atomikos.jdbc.AtomikosDataSourceBean">
      <property name="uniqueResourceName" value="dataSourceZb"/>
      <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
      <property name="xaProperties">
         <props>
            <prop key="user">${userzb}</prop>        
            <prop key="password">${passwordzb}</prop>        
            <prop key="URL">${urlzb}</prop>    
            <prop key="pinGlobalTxToPhysicalConnection">true</prop>       
         </props>
      </property>
      <property name="minPoolSize" value="2"/>
      <property name="maxPoolSize" value="${cpool.maxPoolSize}"/>
      <property name="maxIdleTime" value="${cpool.maxIdleTime}"/>
      <property name="testQuery" value="select 1 "/>
   </bean>
   
   <!-- 交易网的数据库 -->
   <bean id="dataSourceJyw" class="com.atomikos.jdbc.AtomikosDataSourceBean">
      <property name="uniqueResourceName" value="dataSourceJyw"/>
      <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
      <property name="xaProperties">
         <props>
            <prop key="user">${userjyw}</prop>       
            <prop key="password">${passwordjyw}</prop>       
            <prop key="URL">${urljyw}</prop>   
            <prop key="pinGlobalTxToPhysicalConnection">true</prop>       
         </props>
      </property>
      <property name="minPoolSize" value="2"/>
      <property name="maxPoolSize" value="${cpool.maxPoolSize}"/>
      <property name="maxIdleTime" value="${cpool.maxIdleTime}"/>
      <property name="testQuery" value="select 1 "/>
   </bean>
   <!--center数据源  -->
   <bean id="dataSourceCenter" class="com.atomikos.jdbc.AtomikosDataSourceBean">
      <property name="uniqueResourceName" value="dataSourceCenter"/>
      <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/>
      <property name="xaProperties">
         <props>
            <prop key="user">${usercenter}</prop>        
            <prop key="password">${passwordcenter}</prop>        
            <prop key="URL">${urlcenter}</prop>    
            <prop key="pinGlobalTxToPhysicalConnection">true</prop>       
         </props>
      </property>
      <property name="minPoolSize" value="2"/>
      <property name="maxPoolSize" value="${cpool.maxPoolSize}"/>
      <property name="maxIdleTime" value="${cpool.maxIdleTime}"/>
      <property name="testQuery" value="select 1 "/>
   </bean>
   

   <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="jtaTransactionManager" ref="atomikosTransactionManager" /> 
      <property name="hibernateProperties">
         <value>
         <!-- hibernate.dialect=com.zhulong.common.hibernate.dialect.ZhuLongOracleDialect -->
         hibernate.dialect=org.hibernate.dialect.MySQLDialect
         hibernate.show_sql=true
         hibernate.format_sql=true
         hibernate.query.substitutions=true 1, false 0
         hibernate.jdbc.batch_size=20
         hibernate.current_session_context_class=jta
         hibernate.transaction.factory_class=org.hibernate.transaction.JTATransactionFactory
         <!-- hibernate.connection.release_mode=after_statement -->
         </value>
      </property>
      <property name="entityInterceptor" ref="logIntercptor"/>   
      <property name="packagesToScan">
         <list>  
            <value>com.truelore.xunjia.*</value>  
         </list>  
      </property>  
   </bean>
   
   <bean id="sessionFactoryZb" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
      <property name="dataSource" ref="dataSourceZb" />
      <property name="jtaTransactionManager" ref="atomikosTransactionManager" /> 
      <property name="hibernateProperties">
         <value>
         hibernate.dialect=org.hibernate.dialect.MySQLDialect
         hibernate.show_sql=true
         hibernate.format_sql=true
         hibernate.query.substitutions=true 1, false 0
         hibernate.jdbc.batch_size=20
         hibernate.current_session_context_class=jta
         hibernate.transaction.factory_class=org.hibernate.transaction.JTATransactionFactory
         <!-- hibernate.connection.release_mode=after_statement -->
         </value>
      </property>
      <property name="packagesToScan">
         <list>  
            <value>com.truelore.jiaoyi.*</value>  
         </list>  
      </property>  
   </bean>
   
   
   <bean id="sessionFactoryJyw" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
      <property name="dataSource" ref="dataSourceJyw" />
      <property name="jtaTransactionManager" ref="atomikosTransactionManager" /> 
      <property name="hibernateProperties">
         <value>
         hibernate.dialect=org.hibernate.dialect.MySQLDialect
         hibernate.show_sql=true
         hibernate.format_sql=true
         hibernate.query.substitutions=true 1, false 0
         hibernate.jdbc.batch_size=20
         hibernate.current_session_context_class=jta
         hibernate.transaction.factory_class=org.hibernate.transaction.JTATransactionFactory
         <!-- hibernate.connection.release_mode=after_statement -->
         </value>
      </property>
      <property name="packagesToScan">
         <list>  
            <value>com.truelore.jyw.*</value>  
         </list>  
      </property>  
   </bean>
   
   <bean id="sessionFactoryCenter" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
      <property name="dataSource" ref="dataSourceCenter" />
      <property name="jtaTransactionManager" ref="atomikosTransactionManager" /> 
      <property name="hibernateProperties">
         <value>
         hibernate.dialect=org.hibernate.dialect.MySQLDialect
         hibernate.show_sql=false
         hibernate.format_sql=false
         hibernate.query.substitutions=true 1, false 0
         hibernate.jdbc.batch_size=20
         hibernate.current_session_context_class=jta
         hibernate.transaction.factory_class=org.hibernate.transaction.JTATransactionFactory
         <!-- hibernate.connection.release_mode=after_statement -->
         </value>
      </property>
      <property name="packagesToScan">
         <list>  
            <value>com.truelore.center.*</value>  
         </list>  
      </property>  
   </bean>
   
   <!-- 配置事务 -->
   <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
      <property name="forceShutdown" value="true"/>
      <!-- <property name="startupTransactionService" value="true" />
        <property name="transactionTimeout" value="300" /> -->
   </bean>
   
   <!-- Atomikos事务实现 -->
   <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
      <property name="transactionTimeout" value="300"/>
   </bean>
   
   <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
      <property name="transactionManager" ref="atomikosTransactionManager"/>
      <property name="userTransaction" ref="atomikosUserTransaction"/>
      <!-- <property name="allowCustomIsolationLevels" value="true"/> -->
   </bean>
   
   <!-- order 的值越小,越在外围-->
   <tx:annotation-driven transaction-manager="transactionManager" order="1" />
   
   <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
      <property name="templateLoaderPath" value="/html/"/>
      <property name="freemarkerSettings">
         <props>
            <prop key="tag_syntax">auto_detect</prop>
            <prop key="template_update_delay">0</prop>
            <prop key="defaultEncoding">UTF-8</prop>
            <prop key="url_escaping_charset">UTF-8</prop>
            <prop key="locale">zh_CN</prop>
            <prop key="boolean_format">true,false</prop>
            <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
            <prop key="date_format">yyyy-MM-dd</prop>
            <prop key="time_format">HH:mm:ss</prop>
            <prop key="number_format">0.######</prop>
            <prop key="whitespace_stripping">true</prop>
            <!-- <prop key="auto_import">/ftl/pony/index.ftl as p,/ftl/spring.ftl as s</prop> -->
         </props>
      </property>
   </bean>
</beans>

----------

网友的帖子:http://iteye.blog.163.com/blog/static/1863080962012102945116222/



猜你喜欢

转载自blog.csdn.net/u011877584/article/details/80927234