Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Database \ Spring + MyBatis Multi data source switching     - Repair fatal error in Linux: lame / lame.h: No such file or dir Error (Linux)

- Using Android Studio integrated development environment to build Android (Linux)

- Varnish configuration language VCL and its built-in variables (Server)

- How to enable curl command HTTP2 support (Linux)

- Servlet 3.0 interfaces of AsyncListener (Programming)

- Repair CentOS 6.4 Grub boot (Linux)

- Setup Qt development environment under Linux (Linux)

- Remote installation of Net-SNMP whole process (Linux)

- Ubuntu 12.04 installation instructions under GAMIT10.40 (Linux)

- NGINX Plus now fully supports HTTP / 2 (Server)

- Resolve the host via KVM console can not connect clients (Linux)

- Ubuntu UFW firewall settings Introduction (Linux)

- JSON Introduction and Usage Summary (Programming)

- Installation Experience open source car Automotive Grade Linux system (Linux)

- Linux device driver development small example --LED lights (Programming)

- Linux system boot process detail (Linux)

- MySQL 5.6 Open full query log (Database)

- How to view the Linux QPS (Linux)

- Shell scripts quickly deploy Tomcat project (Server)

- Installation of JDK and Tomcat under Linux (CentOS) (Linux)

 
         
  Spring + MyBatis Multi data source switching
     
  Add Date : 2017-08-31      
         
       
         
  In the actual company projects, they are likely to encounter a problem is a Java project, but the project involves two databases on two different IP database still loom.

When this happens, we have two choices

1, do not take the spring aop way, directly to do two dataSource

2, spring management, flexible data source switching

Now come the first two kinds of ways notes:

spring.xml profile:

< ? 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: aop = "http://www.springframework.org/schema/aop"
xmlns: task = "http://www.springframework.org/schema/task" xmlns: tx = "http://www.springframework.org/schema/tx"
xmlns: jdbc = "http://www.springframework.org/schema/jdbc" xmlns: context = "http://www.springframework.org/schema/context"
xsi: schemaLocation = "
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
  
      < ! - Mysql database configuration parameters ->
    < Context: property-placeholder location = "mysql.properties" />
    
    < ! - Configure multiple data sources ->
    < Bean id = "db1" class = "org.apache.commons.dbcp.BasicDataSource"
        destroy-method = "close">
        < ! - Database connection parameters ->
        < Property name = "driverClassName" value = "$ {db1.driver}" />
        < Property name = "url" value = "$ {db1.url}" />
        < Property name = "username" value = "$ {db1.username}" />
        < Property name = "password" value = "$ {db1.password}" />
    
      < ! - Connection Pool Parameters ->
< Property name = "initialSize" value = "10" />
< Property name = "maxActive" value = "500" />
< Property name = "maxIdle" value = "40" />
< Property name = "minIdle" value = "10" />
    < / Bean>
    
    < Bean id = "db2" class = "org.apache.commons.dbcp.BasicDataSource"
        destroy-method = "close">
        < ! - Database connection parameters ->
        < Property name = "driverClassName" value = "$ {db2.driver}" />
        < Property name = "url" value = "$ {db2.url}" />
        < Property name = "username" value = "$ {db2.username}" />
        < Property name = "password" value = "$ {db2.password}" />
    
      < ! - Connection Pool Parameters ->
< Property name = "initialSize" value = "10" />
< Property name = "maxActive" value = "500" />
< Property name = "maxIdle" value = "40" />
< Property name = "minIdle" value = "10" />
    < / Bean>
    
    < Bean id = "dataSource" class = "com.ckd.datasource.mybatis.DynamicDataSource">
        < Property name = "targetDataSources">
            < Map key-type = "java.lang.String">
                < Entry key = "db1" value-ref = "db1" />
                < Entry key = "db2" value-ref = "db2" />
            < / Map>
        < / Property>
        < Property name = "defaultTargetDataSource" ref = "db2" />
    < / Bean>
    
    < -! Mybatis Configuration ->
      < Bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean">
< Property name = "dataSource" ref = "dataSource" />
< Property name = "configLocation" value = "classpath: mybatis / mybatis-config.xml" />
< Property name = "mapperLocations" value = "classpath:. Mybatis / mapper / * xml" />
< / Bean>
< ! - Get sqlSession ->
< Bean id = "sqlSession" class = "org.mybatis.spring.SqlSessionTemplate">
< Constructor-arg index = "0">
        < Ref bean = "sqlSessionFactory" />
        < / Constructor-arg>
< / Bean>
< ! - Transaction Manager configuration, single-source services ->
< Bean id = "transactionManager"
class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
< Property name = "dataSource" ref = "dataSource" />
< / Bean>
    
< / Beans>

-------------------------------------------------- ----------------

/ **
 * @fileName DataSourceContextHolder.java
 * @author Chenkaideng
 * @date 2015 Nian Yue 27 Ri. 8
 * @describe Data source setter Holder class
 * /
public class DataSourceContextHolder {
 
  private static final ThreadLocal contextHolder = new ThreadLocal ();
  
  public static void setDbType (String dbType) {
  contextHolder.set (dbType);
  }
  
  public static String getDbType () {
  return ((String) contextHolder.get ());
  }
  
  public static void clearDbType () {
  contextHolder.remove ();
  }
}

Get that done more things, the rest of the things simple

- "First, load the file spring.xml applicationContext = new ClassPathXmlApplicationContext (" spring.xml ");

- "And then set the data source DataSourceContextHolder.setDbType (" db1 ");

- "Then get sqlSession = (SqlSession) applicationContext.getBean from applicationContext in (" sqlSession ");

- "Finally, you can take this sqlSession do CRUD operations

Note: Do not do this sqlSession close and comit operation since the spring have their own administration, do not do these operations manually.
     
         
       
         
  More:      
 
- Linux Demo dd IO test (Linux)
- Firewall Configuration Red Hat Enterprise Linux 4 (Linux)
- Oracle Database asynchronous IO cause slow query response (Database)
- Android Fragment everything you need to know (Programming)
- Python Flask environment to build (Linux)
- Let Mac OS X dedicated high-speed mobile hard disk can also be read in Linux (Linux)
- Node.js Getting the basics: Helloworld! (Linux)
- DRBD-based installation and configuration of CentOS 6.5 (Server)
- Oracle to start to solve the error ORA-27102 (Database)
- Ubuntu 14.10 Install Ubuntu Touch Music App 2.0 (Linux)
- Linux systems use IP masquerading anti-hacker (Linux)
- The principle Httpclient4.4 (execution request) (Programming)
- Several start-up mode of Tomcat (Server)
- Ceph single / multi-node installation summary Powered by CentOS 6.x (Server)
- How to use SHA256 checksum files download (Linux)
- PostgreSQL-XL installation and deployment (Database)
- S5PV210 development board for embedded development environment to build under Ubuntu (Linux)
- Linux smart small switch rfkill (Linux)
- Dell R710 server disk recovery database one case (record) (Server)
- OpenSUSE GPG Comments (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.