Giter Site home page Giter Site logo

perseus's Introduction

Join the chat at https://gitter.im/chengdedeng/perseus

Project description

The read/write separation of databases is a basic requirement, it is usually comes in three ways:

  1. multi-data source,hard-code.
  2. Extended ORM.
  3. Implement protocol of database by middleware.

First is simplest,but developer need to do a lot of work and easy to make mistakes;although the third program is transparent for developer and don't restrict the programming language, but it's most difficult of development and the scope of the database support is fewer. This project is based on the program II, select Mybatis and Spring of most popular framework in java, so it is only applicable to the Mybatis + Spring implementation of the Java project.

Function

  1. The transaction is routed to the master database and does't distinguish whether transaction is readonly. Due to readonly don't really start the transaction, just activate the transaction synchronization, so don't be DynamicDataSourceTransactionManager to intercept, so setting to be hit by the default(master database), and the execution of all queries in a read only transaction will reuse the same JDBCConnection(SqlSession).
  2. The select query is routed to the slave database, insert/update/delete SQL routing to the main master database.
  3. Support select query to force routing to master database (as far as possible, bypassing business logic optimization).
  4. Support batch operations in mybatis-spring.

Stability

The project is widely used in the author's company, hundreds of projects, and has been well developed, with detailed configuration and test code in the test code.

Core configuration

General configuration

    <bean id="dataSource" class="info.yangguo.perseus.DynamicDataSource">
        <property name="master" ref="master"/>
        <property name="slaves">
            <list>
                <ref bean="slave1"/>
                <ref bean="slave2"/>
            </list>
        </property>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="info.yangguo.perseus.test.domain"/>
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
        <property name="mapperLocations" value="classpath:info/yangguo/perseus/test/dao/*.xml"/>
    </bean>

    <bean class="info.yangguo.perseus.MapperScannerConfigurer">
        <property name="basePackage" value="info.yangguo.perseus.test.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

    <bean id="transactionManager"
          class="info.yangguo.perseus.DynamicDataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

Batch operation configuration

    <bean id="dataSource" class="info.yangguo.perseus.DynamicDataSource">
        <property name="master" ref="master"/>
        <property name="slaves">
            <list>
                <ref bean="slave1"/>
                <ref bean="slave2"/>
            </list>
        </property>
    </bean>

    <!-- transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
          class="info.yangguo.perseus.DynamicDataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- enable transaction demarcation with annotations -->
    <tx:annotation-driven/>

    <!-- simplest possible SqlSessionFactory configuration -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:EmployeeMapper.xml"/>
    </bean>
    <!-- item reader  -->
    <bean id="pagingNoNestedItemReader" class="info.yangguo.perseus.DynamicMyBatisPagingItemReader">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        <property name="sqlSessionTemplate" ref="dynamicSqlSession"/>
        <property name="queryId" value="getEmployeeNoNestedPaging"/>
        <property name="pageSize" value="5"/>
    </bean>

    <bean id="pagingNestedItemReader" class="info.yangguo.perseus.DynamicMyBatisPagingItemReader">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        <property name="sqlSessionTemplate" ref="dynamicSqlSession"/>
        <property name="queryId" value="getEmployeeNestedPaging"/>
        <property name="pageSize" value="5"/>
    </bean>

    <bean id="cursorNoNestedItemReader" class="info.yangguo.perseus.DynamicMyBatisCursorItemReader">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        <property name="queryId" value="getEmployeeNoNestedCursor"/>
    </bean>

    <bean id="cursorNestedItemReader" class="info.yangguo.perseus.DynamicMyBatisCursorItemReader">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        <property name="queryId" value="getEmployeeNestedCursor"/>
    </bean>

    <bean id="writer" class="org.mybatis.spring.batch.MyBatisBatchItemWriter">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        <property name="statementId" value="updateEmployee"/>
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
        <constructor-arg index="1" value="BATCH"/>
    </bean>
    <bean id="dynamicSqlSession" class="info.yangguo.perseus.DynamicSqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSession"/>
    </bean>

perseus's People

Contributors

chengdedeng avatar gitter-badger avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.