工作总结(鄂州商城数据库配置)————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

相关文章

SpringBoot请求参数接收控制指南分享

《SpringBoot请求参数接收控制指南分享》:本文主要介绍SpringBoot请求参数接收控制指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring Boot 请求参数接收控制指南1. 概述2. 有注解时参数接收方式对比3. 无注解时接收参数默认位置

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

Spring Boot 整合 SSE的高级实践(Server-Sent Events)

《SpringBoot整合SSE的高级实践(Server-SentEvents)》SSE(Server-SentEvents)是一种基于HTTP协议的单向通信机制,允许服务器向浏览器持续发送实... 目录1、简述2、Spring Boot 中的SSE实现2.1 添加依赖2.2 实现后端接口2.3 配置超时时

Spring Boot读取配置文件的五种方式小结

《SpringBoot读取配置文件的五种方式小结》SpringBoot提供了灵活多样的方式来读取配置文件,这篇文章为大家介绍了5种常见的读取方式,文中的示例代码简洁易懂,大家可以根据自己的需要进... 目录1. 配置文件位置与加载顺序2. 读取配置文件的方式汇总方式一:使用 @Value 注解读取配置方式二

一文详解Java异常处理你都了解哪些知识

《一文详解Java异常处理你都了解哪些知识》:本文主要介绍Java异常处理的相关资料,包括异常的分类、捕获和处理异常的语法、常见的异常类型以及自定义异常的实现,文中通过代码介绍的非常详细,需要的朋... 目录前言一、什么是异常二、异常的分类2.1 受检异常2.2 非受检异常三、异常处理的语法3.1 try-

Java中的@SneakyThrows注解用法详解

《Java中的@SneakyThrows注解用法详解》:本文主要介绍Java中的@SneakyThrows注解用法的相关资料,Lombok的@SneakyThrows注解简化了Java方法中的异常... 目录前言一、@SneakyThrows 简介1.1 什么是 Lombok?二、@SneakyThrows

Java中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

如何为Yarn配置国内源的详细教程

《如何为Yarn配置国内源的详细教程》在使用Yarn进行项目开发时,由于网络原因,直接使用官方源可能会导致下载速度慢或连接失败,配置国内源可以显著提高包的下载速度和稳定性,本文将详细介绍如何为Yarn... 目录一、查询当前使用的镜像源二、设置国内源1. 设置为淘宝镜像源2. 设置为其他国内源三、还原为官方

Spring 请求之传递 JSON 数据的操作方法

《Spring请求之传递JSON数据的操作方法》JSON就是一种数据格式,有自己的格式和语法,使用文本表示一个对象或数组的信息,因此JSON本质是字符串,主要负责在不同的语言中数据传递和交换,这... 目录jsON 概念JSON 语法JSON 的语法JSON 的两种结构JSON 字符串和 Java 对象互转