工作总结(鄂州商城数据库配置)————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集成Lucene的详细指南

《springboot集成Lucene的详细指南》这篇文章主要为大家详细介绍了springboot集成Lucene的详细指南,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起... 目录添加依赖创建配置类创建实体类创建索引服务类创建搜索服务类创建控制器类使用示例以下是 Spring

Java调用Python的四种方法小结

《Java调用Python的四种方法小结》在现代开发中,结合不同编程语言的优势往往能达到事半功倍的效果,本文将详细介绍四种在Java中调用Python的方法,并推荐一种最常用且实用的方法,希望对大家有... 目录一、在Java类中直接执行python语句二、在Java中直接调用Python脚本三、使用Run

Java根据IP地址实现归属地获取

《Java根据IP地址实现归属地获取》Ip2region是一个离线IP地址定位库和IP定位数据管理框架,这篇文章主要为大家详细介绍了Java如何使用Ip2region实现根据IP地址获取归属地,感兴趣... 目录一、使用Ip2region离线获取1、Ip2region简介2、导包3、下编程载xdb文件4、J

浅析如何使用xstream实现javaBean与xml互转

《浅析如何使用xstream实现javaBean与xml互转》XStream是一个用于将Java对象与XML之间进行转换的库,它非常简单易用,下面将详细介绍如何使用XStream实现JavaBean与... 目录1. 引入依赖2. 定义 JavaBean3. JavaBean 转 XML4. XML 转 J

SpringBoot中四种AOP实战应用场景及代码实现

《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO... 目录引言场景一:日志记录与性能监控业务需求实现方案使用示例扩展:MDC实现请求跟踪场景二:权限控制与

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

MySQL数据库约束深入详解

《MySQL数据库约束深入详解》:本文主要介绍MySQL数据库约束,在MySQL数据库中,约束是用来限制进入表中的数据类型的一种技术,通过使用约束,可以确保数据的准确性、完整性和可靠性,需要的朋友... 目录一、数据库约束的概念二、约束类型三、NOT NULL 非空约束四、DEFAULT 默认值约束五、UN

Java 实用工具类Spring 的 AnnotationUtils详解

《Java实用工具类Spring的AnnotationUtils详解》Spring框架提供了一个强大的注解工具类org.springframework.core.annotation.Annot... 目录前言一、AnnotationUtils 的常用方法二、常见应用场景三、与 JDK 原生注解 API 的