jpa下Tomcat jdbc pool 读写分离问题

xujianping32 2017-05-11 10:43:30
近来要做数据库读写分离功能,后台是三层架构controller -- service(事务层) -- dao ,根据一些例子整理了下。一些配置的代码如下:
<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd"
default-lazy-init="true">

<description>Spring公共配置 </description>

<!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
<context:component-scan base-package="com.xx">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>

<!-- Jpa Entity Manager 配置 -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<property name="packagesToScan" value="com.xx" />
<property name="jpaProperties">
<props>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="net.sf.ehcache.configurationResourceName">cache/ehcache-hibernate-local.xml</prop>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.use_sql_commants">true</prop>
<prop key="hibernate.jdbc.batch_size">50</prop>
<prop key="hibernate.dialect">com.xx.base.dialect.CustomMySQL5Dialect</prop>
</props>
</property>
</bean>

<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform">
<bean factory-method="getDialect" class="org.springside.modules.persistence.Hibernates">
<constructor-arg ref="dataSource" />
</bean>
</property>
</bean>

<!-- Spring Data Jpa配置 -->
<jpa:repositories base-package="com.xx" transaction-manager-ref="transactionManager"
entity-manager-factory-ref="entityManagerFactory" />

<!-- Jpa 事务配置 -->
<bean id="transactionManager" class="com.xx.base.dataSource.MyJpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<!-- 定义aspectj -->
<aop:aspectj-autoproxy />

<!-- hibernate Validator定义 -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

<!-- controller读取配置文件 -->
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:application.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties" />
</bean>

<!-- production环境 -->
<beans profile="production">
<context:property-placeholder ignore-unresolvable="true" location="classpath*:/application.properties" />

<bean id="dataSource" class="com.xx.base.dataSource.RwDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<!-- write -->
<entry key="master" value-ref="masterDataSource"/>
<!-- read -->
<entry key="slave" value-ref="slaveDataSource"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="masterDataSource"/>
</bean>

<!-- 数据源配置, 使用Tomcat JDBC连接池 主(写)数据源 -->
<bean id="masterDataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Connection Pooling Info -->
<property name="maxActive" value="${jdbc.pool.maxActive}" />
<property name="maxIdle" value="${jdbc.pool.maxIdle}" />
<property name="minIdle" value="0" />
<property name="defaultAutoCommit" value="false" />
<!-- 连接Idle10分钟后超时,每1分钟检查一次 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="600000" />
<property name="testWhileIdle" value="true"/>
<property name="validationQuery" value="select database()" />
</bean>

<!-- 数据源配置, 使用Tomcat JDBC连接池 从(只读)数据源 -->
<bean id="slaveDataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${slave.jdbc.url}" />
<property name="username" value="${slave.jdbc.username}" />
<property name="password" value="${slave.jdbc.password}" />
<!-- Connection Pooling Info -->
<property name="maxActive" value="${slave.jdbc.pool.maxActive}" />
<property name="maxIdle" value="${slave.jdbc.pool.maxIdle}" />
<property name="minIdle" value="0" />
<property name="defaultAutoCommit" value="false" />
<!-- 连接Idle10分钟后超时,每1分钟检查一次 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="600000" />
<property name="testWhileIdle" value="true"/>
<property name="validationQuery" value="select database()" />
</bean>

</beans>

</beans>


MyJpaTransactionManager:
package com.xx.base.dataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.support.DefaultTransactionStatus;

@SuppressWarnings("serial")
public class MyJpaTransactionManager extends JpaTransactionManager{
private static final Logger logger = LoggerFactory.getLogger(MyJpaTransactionManager.class);
@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
if(definition.isReadOnly()){
RwDataSourceHolder.localSlave();
}else{
RwDataSourceHolder.localMaster();
}
logger.info("jpa-transaction:begin-----now dataSource is ["+RwDataSourceHolder.getDataSouce()+"]");
super.doBegin(transaction, definition);
}
@Override
protected void doCommit(DefaultTransactionStatus status) {
logger.info("jpa-transaction:commit-----now dataSource is ["+RwDataSourceHolder.getDataSouce()+"]");
super.doCommit(status);
}
}


package com.xx.base.dataSource;
import java.sql.Connection;
import java.sql.SQLException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class RwDataSource extends AbstractRoutingDataSource {
private static final Logger logger = LoggerFactory.getLogger(RwDataSource.class);
@Override
public Connection getConnection() throws SQLException
{
return determineTargetDataSource().getConnection();
}
@Override
protected Object determineCurrentLookupKey() {
logger.info("dataSource: " + RwDataSourceHolder.getDataSouce());
return RwDataSourceHolder.getDataSouce();
}
}

package com.xx.base.dataSource;
public class RwDataSourceHolder {
public static final String MASTER = "master"; //主(写)连接池
public static final String SLAVE = "slave"; //从(读)连接池
public static final ThreadLocal<String> holder = new ThreadLocal<String>();
public static void localMaster() {
holder.set(MASTER);
}

public static void localSlave() {
holder.set(SLAVE);
}

public static void clear() {
holder.remove();
}

public static String getDataSouce() {
return holder.get();
}
}


运行后,发现数据源的切换并没有随着事务切换。一个controller可能调用多个service的方法,也就是有多个事务。但是只有在调用第一个事务的时候,才会去切换数据源。比如controller有A,B两个事务,日志如下:
jpa-transaction:begin-----now dataSource is [slave]
dataSource:slave
jpa-transaction:commit-----now dataSource is [slave]
jpa-transaction:begin-----now dataSource is [master]
jpa-transaction:commit-----now dataSource is [master]

前三行是A事务,会切换数据源。而后两行是B事务,没有再切换数据源了。导致当前数据源还是只读的,就报错了。
...全文
251 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
X元素 2017-05-15
  • 打赏
  • 举报
回复
引用 7 楼 xujianping32 的回复:
[quote=引用 6 楼 u011619071 的回复:] [quote=引用 5 楼 xujianping32 的回复:] [quote=引用 4 楼 u011619071 的回复:] [quote=引用 3 楼 xujianping32 的回复:] [quote=引用 2 楼 u011619071 的回复:] [quote=引用 1 楼 xujianping32 的回复:] 没有人遇到过类似的情况?
使用的哪种数据库?[/quote] rds 的mysql[/quote] 考虑一下mysql的Replication [/quote] 就是不从代码上做,到mysql上配置主从复制?[/quote] 如果你的mysql 已经存在主从实例了,直接使用Replication jar ,就可以实现读写分离了[/quote] 驱动改成 : com.mysql.jdbc.ReplicationDriver ? 报错了:java.lang.ClassNotFoundException: com.mysql.jdbc.ReplicationDriver jar 是mysql-connector-java-5.1.36[/quote] https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-master-slave-replication-connection.html 看一下官方文档。
xujianping32 2017-05-14
  • 打赏
  • 举报
回复
引用 6 楼 u011619071 的回复:
[quote=引用 5 楼 xujianping32 的回复:] [quote=引用 4 楼 u011619071 的回复:] [quote=引用 3 楼 xujianping32 的回复:] [quote=引用 2 楼 u011619071 的回复:] [quote=引用 1 楼 xujianping32 的回复:] 没有人遇到过类似的情况?
使用的哪种数据库?[/quote] rds 的mysql[/quote] 考虑一下mysql的Replication [/quote] 就是不从代码上做,到mysql上配置主从复制?[/quote] 如果你的mysql 已经存在主从实例了,直接使用Replication jar ,就可以实现读写分离了[/quote] 驱动改成 : com.mysql.jdbc.ReplicationDriver ? 报错了:java.lang.ClassNotFoundException: com.mysql.jdbc.ReplicationDriver jar 是mysql-connector-java-5.1.36
xujianping32 2017-05-12
  • 打赏
  • 举报
回复
引用 4 楼 u011619071 的回复:
[quote=引用 3 楼 xujianping32 的回复:] [quote=引用 2 楼 u011619071 的回复:] [quote=引用 1 楼 xujianping32 的回复:] 没有人遇到过类似的情况?
使用的哪种数据库?[/quote] rds 的mysql[/quote] 考虑一下mysql的Replication [/quote] 就是不从代码上做,到mysql上配置主从复制?
X元素 2017-05-12
  • 打赏
  • 举报
回复
引用 3 楼 xujianping32 的回复:
[quote=引用 2 楼 u011619071 的回复:] [quote=引用 1 楼 xujianping32 的回复:] 没有人遇到过类似的情况?
使用的哪种数据库?[/quote] rds 的mysql[/quote] 考虑一下mysql的Replication
xujianping32 2017-05-12
  • 打赏
  • 举报
回复
引用 2 楼 u011619071 的回复:
[quote=引用 1 楼 xujianping32 的回复:] 没有人遇到过类似的情况?
使用的哪种数据库?[/quote] rds 的mysql
X元素 2017-05-12
  • 打赏
  • 举报
回复
引用 5 楼 xujianping32 的回复:
[quote=引用 4 楼 u011619071 的回复:] [quote=引用 3 楼 xujianping32 的回复:] [quote=引用 2 楼 u011619071 的回复:] [quote=引用 1 楼 xujianping32 的回复:] 没有人遇到过类似的情况?
使用的哪种数据库?[/quote] rds 的mysql[/quote] 考虑一下mysql的Replication [/quote] 就是不从代码上做,到mysql上配置主从复制?[/quote] 如果你的mysql 已经存在主从实例了,直接使用Replication jar ,就可以实现读写分离了
X元素 2017-05-11
  • 打赏
  • 举报
回复
引用 1 楼 xujianping32 的回复:
没有人遇到过类似的情况?
使用的哪种数据库?
xujianping32 2017-05-11
  • 打赏
  • 举报
回复
没有人遇到过类似的情况?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧