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

本文主要是介绍工作总结(鄂州商城数据库配置)————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.MySQLDialecthibernate.show_sql=truehibernate.format_sql=truehibernate.query.substitutions=true 1, false 0hibernate.jdbc.batch_size=20hibernate.current_session_context_class=jtahibernate.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.MySQLDialecthibernate.show_sql=truehibernate.format_sql=truehibernate.query.substitutions=true 1, false 0hibernate.jdbc.batch_size=20hibernate.current_session_context_class=jtahibernate.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.MySQLDialecthibernate.show_sql=truehibernate.format_sql=truehibernate.query.substitutions=true 1, false 0hibernate.jdbc.batch_size=20hibernate.current_session_context_class=jtahibernate.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.MySQLDialecthibernate.show_sql=truehibernate.format_sql=truehibernate.query.substitutions=true 1, false 0hibernate.jdbc.batch_size=20hibernate.current_session_context_class=jtahibernate.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.MySQLDialecthibernate.show_sql=falsehibernate.format_sql=falsehibernate.query.substitutions=true 1, false 0hibernate.jdbc.batch_size=20hibernate.current_session_context_class=jtahibernate.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/



这篇关于工作总结(鄂州商城数据库配置)————spring配置多数据源+atomikos分布式事务的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1073758

相关文章

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2