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

相关文章

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("