Spring +SpringMvc+Mybatis+Mysql+SqlServer+Atomikos+transactions.properties

系统场景是这样的,因业务需要配置多数据源进行大数据批量插入到数据库中,就是这配置多数据源的时候要考虑到事物的处理

事物的处理有很多种方式在网上查询了很多资料遇过很多的坑结合自己的使用情况整理一下,大概花了一周的时间,真的是悲催的要命,说 到这有点脑壳疼。如果想要去了解其中的原理请在网上收一下吧我就不在copy了。。。。

多数据源Mapper文件映射的配置方式如果要配置事物管理需要依赖spring的事物管理器。我先介绍Atomikos+jta的方式实现吧。

实现分布式事物数据源框架的几个类型网上查资料是三种我这里试了两种类型的:

com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean:
(说这种不支持分布式事物具体还未测试验证但是单数据源我这里是可以回滚的事物配置到了dao层)-》一般的事物管理都在service层管理但是我这里试了几种方式都是在Dao层生效的。抛异常回滚。
com.atomikos.jdbc.AtomikosDataSourceBean :
这个可以支持分布式事物,就是保持数据业务的一致性完整性。
这种方式可以配合阿里的com.alibaba.druid.pool.xa.DruidXADataSource 数据源连接池使用(但是要把oracle,mysql ,sqlserver的配置引用要分开 也许是当时引用的是sqlserver的驱动包不对在进行驱动设置的时候报错了找不到dbType为sqlserver的。)要使用分布式事物首先要选对正确的jar包驱动,首先要实现XADataSource接口。下图是实现了XADataSource的包mysql的sqlserver和jtds类型的。和数据源连接池dbType代码判断。

配置项目钱需要的jar包文件:

maven配置如下:


   cglib
   cglib-nodep
   3.2.2



   org.springframework
   spring-web
   4.2.4


   org.springframework
   spring-webmvc
  4.2.4


   org.springframework
   spring-context-support
   4.2.4


   org.springframework
   spring-jdbc
   4.2.4


   org.springframework
   spring-tx
   ${org.springframework.version}


   org.springframework.data
   spring-data-mongodb
   1.8.1.RELEASE



   com.alibaba
   druid
   1.1.12


   mysql
   mysql-connector-java
   5.1.23


   org.apache.tomcat
   tomcat-jdbc
   compile
   7.0.57}

###################################################################################

首先是com.atomikos.jdbc.AtomikosDataSourceBean 完整版可以完全使用的方式:下面上配置文件:

       xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop = "http://www.springframework.org/schema/aop"
       xmlns:tx = "http://www.springframework.org/schema/tx"
       xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
       default-autowire = "byName"   default-lazy-init = "true" >


   
       
         
       
       
       
       
         
       
       
       
       
       
           
                ${jdbc.jdbcUrl}
                ${jdbc.password}
                ${jdbc.username}
           

       

   

   
       
         
       
       
       
       
       
       
       
       
       
       
           

                    ${h.jdts.user}
                    ${h.jdts.password}
                    ${h.jdts.serverName}
                    ${h.jdts.portNumber}
                    ${h.jdts.databaseName}

           
       
   
   
       
       
       
       
       
       
       
       
       
       
       
       

           

                ${x.jdts.user}
                ${x.jdts.password}
                ${x.jdts.serverName}
                ${x.jdts.portNumber}
                ${x.jdts.databaseName}

           
       
   
   
       
         
       
       
       
       
       
       
       
       
       
       
           

                ${d.jdts.user}
                ${d.jdts.password}
                ${d.jdts.serverName}
                ${d.jdts.portNumber}
                ${d.jdts.databaseName}

           
       
   


   
   
       
       
       
           
                classpath*:mybatis/mysql/*.xml
           

       

   

   
       
       
       
           
                classpath*:mybatis/sqlserver/*.xml       
           

       

   

   
       
       
       
           
                classpath*:mybatis/xsqlserver/*.xml
           

       

   

   
       
       
       
           
                classpath*:mybatis/dsqlserver/*.xml
           

       

   

   
   
   

       
       

   
   
       
       

   
   
       
       

   
   
       
       
   


   
   
       
            true
       

   
    

 
       
       
   

   

       


引用的变量 validationQuery=SELECT 1 
配置在jdbc.properties 名字可以随便起在项目启动的时候需要把配置文件加载进来。参数解释:

        ——》唯一的资源名称,自己起
        -》数据源类型驱动包,不同的数据源类型加载不同的数据源包》 
          -》线程池数量
        最小线程池数量,如果用哦jta.jdbc.properties的时候这个并未生效,而是加载的配置文件总的线程池。
        最大线程池数量
         
       
        ->
          登陆超时时间。如果超过这个时间还未连接上就会报超时错误。
        测试数据连接。

testquery参数的详解可参考 :https://blog.csdn.net/fbysss/article/details/5393626

这个些数据库连接参数要配置在配置文件jdbc.properties 中举一个例子:

x.jdts.serverName=127.0.0.1
x.jdts.portNumber=1433
x.jdts.databaseName=数据库实例名称。
x.jdts.user=root
x.jdts.password=1234

注意:容易犯错误的地方。。。。。。。。一定要注意哦。

sqlserver的配置和mysql的配置不一样因为他们的驱动包里的属性是不一样的如果配置错误会报类似错误:”

set  property password  Error(key的名字也就是没有这个属性所以要注意的要点就是根据驱动包属性配置key)

           

                ${x.jdts.user}         
                ${x.jdts.password}
                ${x.jdts.serverName}
                ${x.jdts.portNumber}
                ${x.jdts.databaseName}

           
       

Mysql配置,注意啊这参数和sqlserver不一样。

   
           
                ${jdbc.jdbcUrl}
                ${jdbc.password}
                ${jdbc.username}
           

       

配置完成项目了我们要设置 transactions.properties 或者是jta.properties  (两个文件名取一个,因为在加载的时候首先默认去加载transactions,如果不存在去加载jta,否则就是默认的配置文件在这个文件中你可以设置日志输出的路径,事物管理最大超时时间等)这个文件要放在classpath下面也就是你的resource的资源路径的跟路径下,一定要放对地方哦,放不对不加载,还是jta默认的。

transactions.properties 贴上我的配置。

#自定义设置jta属性
com.atomikos.icatch.automatic_resource_registration=true
com.atomikos.icatch.client_demarcation=false
com.atomikos.icatch.threaded_2pc=false
com.atomikos.icatch.serial_jta_transactions=true
com.atomikos.icatch.serializable_logging=true
com.atomikos.icatch.log_base_dir=\\data\\logs\\atomikos\\
com.atomikos.icatch.max_actives=50
com.atomikos.icatch.checkpoint_interval=500
com.atomikos.icatch.enable_logging=true
com.atomikos.icatch.output_dir=\\data\\logs\\atomikos\\
com.atomikos.icatch.log_base_name=tm
com.atomikos.icatch.console_file_name = tm.out
com.atomikos.icatch.max_timeout=3000000
#com.atomikos.icatch.tm_unique_name=tm
java.naming.factory.initial=com.sun.jndi.rmi.registry.RegistryContextFactory
java.naming.provider.url=rmi://localhost:1099
com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory
com.atomikos.icatch.force_shutdown_on_vm_exit=false
com.atomikos.icatch.default_jta_timeout=100000
com.atomikos.icatch.console_log_level = ERROR 
com.atomikos.icatch.tm_unique_name = tm
#com.atomikos.icatch.serializable_logging=false
#com.atomikos.icatch.enable_logging=false

# SAMPLE PROPERTIES FILE FOR THE TRANSACTION SERVICE
# THIS FILE ILLUSTRATES THE DIFFERENT SETTINGS FOR THE TRANSACTION MANAGER
# UNCOMMENT THE ASSIGNMENTS TO OVERRIDE DEFAULT VALUES;
# Required: factory implementation class of the transaction core.
# NOTE: there is no default for this, so it MUST be specified!
#官网jta默认属性配置.

#com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory

        
# Set base name of file where messages are output 
# (also known as the 'console file').
#
# com.atomikos.icatch.console_file_name = tm.out

# Size limit (in bytes) for the console file;
# negative means unlimited.
#
# com.atomikos.icatch.console_file_limit=-1

# For size-limited console files, this option
# specifies a number of rotating files to 
# maintain.
#
# com.atomikos.icatch.console_file_count=1

# Set the number of log writes between checkpoints
#
# com.atomikos.icatch.checkpoint_interval=500

# Set output directory where console file and other files are to be put
# make sure this directory exists!
#
# com.atomikos.icatch.output_dir = ./

# Set directory of log files; make sure this directory exists!
#
# com.atomikos.icatch.log_base_dir = ./

# Set base name of log file
# this name will be  used as the first part of 
# the system-generated log file name
#
# com.atomikos.icatch.log_base_name = tmlog

# Set the max number of active local transactions 
# or -1 for unlimited.
#
# com.atomikos.icatch.max_actives = 50

# Set the default timeout (in milliseconds) for local transactions
#
# com.atomikos.icatch.default_jta_timeout = 10000

# Set the max timeout (in milliseconds) for local transactions
#
# com.atomikos.icatch.max_timeout = 300000

# The globally unique name of this transaction manager process
# override this value with a globally unique name
#
# com.atomikos.icatch.tm_unique_name = tm
    
# Do we want to use parallel subtransactions? JTA's default
# is NO for J2EE compatibility
#
# com.atomikos.icatch.serial_jta_transactions=true
                    
# If you want to do explicit resource registration then
# you need to set this value to false.
#
# com.atomikos.icatch.automatic_resource_registration=true  
    
# Set this to WARN, INFO or DEBUG to control the granularity
# of output to the console file.
#
# com.atomikos.icatch.console_log_level=WARN
    
# Do you want transaction logging to be enabled or not?
# If set to false, then no logging overhead will be done
# at the risk of losing data after restart or crash.
#
# com.atomikos.icatch.enable_logging=true

# Should two-phase commit be done in (multi-)threaded mode or not?
# Set this to false if you want commits to be ordered according
# to the order in which resources are added to the transaction.
#
# NOTE: threads are reused on JDK 1.5 or higher. 
# For JDK 1.4, thread reuse is enabled as soon as the 
# concurrent backport is in the classpath - see 
# http://mirrors.ibiblio.org/pub/mirrors/maven2/backport-util-concurrent/backport-util-concurrent/
#
# com.atomikos.icatch.threaded_2pc=false

# Should shutdown of the VM trigger shutdown of the transaction core too?
#
# com.atomikos.icatch.force_shutdown_on_vm_exit=false

配置完属性是不是想知道日志是怎么输出的呢,我的项目用的是logback的如果你用的是其他方式的也是大同小异,需要和com.atomikos 进行关联设置。

 



   
   

   
   
   
        true
        ${logPath}/${baseName}_all.log
       
            ${logPath}/archive/${baseName}_all-%d{yyyy-MM-dd}.log.zip
            60
       

       
            [%-5p] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [%C] - %m%n
       

   

   
       
            INFO
       

        1048576
        0
        true
       
   


   
   
        true
        ${logPath}/archive/${baseName}_interaction.log
       
            ${logPath}/archive/${baseName}_interaction-%d{yyyy-MM-dd}-%i.log.zip
            60
       

       
            [%-5p] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [%C] - %m%n
       

   

   
        1048576
        0
        true
       
   


   
        
       
        
   


   
      
       
       
   

    
   
        
       
        
   


   
       
              
   


以上是使用过程中没有问题AtomikosDataSourceBean ,其实在配置过程中难免会有错误导致运行不起来,实在找不出错误来抓紧时间去休息一下睡个觉洗个澡去锻炼锻炼,第二天来到马上就搞定了,我弄了好久弄的脑袋晕,一看全是泪啊,感觉血压突然就上来了。加油共勉大家一起努力,实在不行老子不弄了咋地!还有两种方式没有写完,后续在写,老子脑袋疼了不弄了我去撸铁去了。

 

 

你可能感兴趣的:(Spring,elastic-job,SpringMvc)