日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

java dbtype_Java实现数据库的读写分离

發(fā)布時(shí)間:2025/3/15 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java dbtype_Java实现数据库的读写分离 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

引言

1、讀寫分離:可以通過Spring提供的AbstractRoutingDataSource類,重寫determineCurrentLookupKey方法,實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源的功能;讀寫分離可以有效減輕寫庫(kù)的壓力,又可以把查詢數(shù)據(jù)的請(qǐng)求分發(fā)到不同讀庫(kù);

2、寫數(shù)據(jù)庫(kù):當(dāng)調(diào)用insert、update、delete及一些實(shí)時(shí)數(shù)據(jù)用到的庫(kù);

3、讀數(shù)據(jù)庫(kù):當(dāng)調(diào)用select查詢數(shù)據(jù)用到的庫(kù);

4、JaveWeb工程通過AbstractRoutingDataSource類實(shí)現(xiàn)讀寫分離;

一、jdbc.properties文件配置讀寫數(shù)據(jù)源

datasource.type=mysql

datasource.driverClassName=com.mysql.jdbc.Driver

datasource.username=root

#寫庫(kù)w.datasource.url=jdbc\:mysql\://127.0.0.1\:3306/ddt?characterEncoding\=utf-8w.datasource.password=write123

#讀庫(kù)r.datasource.url=jdbc\:mysql\://IP\:3306/ddt?characterEncoding\=utf-8r.datasource.password=read123

#連接池配置

c3p0.acquireIncrement=3

c3p0.acquireRetryAttempts=10

c3p0.acquireRetryDelay=1000

c3p0.initialPoolSize=20

c3p0.idleConnectionTestPeriod=3600

c3p0.testConnectionOnCheckout=true

c3p0.minPoolSize=10

c3p0.maxPoolSize=80

c3p0.maxStatements=100

c3p0.numHelperThreads=10

c3p0.maxIdleTime=10800

二、application.xml文件

<?xml version="1.0" encoding="UTF-8"?>

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"?xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-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/jee

http://www.springframework.org/schema/jee/spring-jee-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/task

http://www.springframework.org/schema/task/spring-task-3.0.xsd">????

classpath*:jdbc.properties

destroy-method="close">

${datasource.driverClassName}

${r.datasource.url}

${datasource.username}

${r.datasource.password}

${c3p0.acquireIncrement}

${c3p0.acquireRetryAttempts}

${c3p0.acquireRetryDelay}

${c3p0.initialPoolSize}

${c3p0.testConnectionOnCheckout}

${c3p0.minPoolSize}

${c3p0.maxPoolSize}

${c3p0.maxIdleTime}

${c3p0.idleConnectionTestPeriod}

${c3p0.maxStatements}

${c3p0.numHelperThreads}

destroy-method="close">

${datasource.driverClassName}

${w.datasource.url}

${datasource.username}

${w.datasource.password}

${c3p0.acquireIncrement}

${c3p0.acquireRetryAttempts}

${c3p0.acquireRetryDelay}

${c3p0.initialPoolSize}

${c3p0.testConnectionOnCheckout}

${c3p0.minPoolSize}

${c3p0.maxPoolSize}

${c3p0.maxIdleTime}

${c3p0.idleConnectionTestPeriod}

${c3p0.maxStatements}

${c3p0.numHelperThreads}

class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

${hibernate.dialect}

${hibernate.connection.autocommit}

${hibernate.show_sql}

${hibernate.format_sql}

${hibernate.hbm2ddl.auto}

utf-8

${hibernate.cache.user_query_cache}

${hibernate.user_second_level_cache}

${hibernate.cache.class}

${hibernate.ehcache_config_file}

三、繼承AbstractRoutingDataSource類的動(dòng)態(tài)數(shù)據(jù)源類DynamicDataSource

package?com.eb3.ddt;

import?org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public?class?DynamicDataSource extends?AbstractRoutingDataSource {

/**

*?重寫determineCurrentLookupKey方法

*/

@Override

protected?Object determineCurrentLookupKey() {

Object obj = DBHelper.getDbType();

return?obj;

}

}

四、DBHelper工具類

package?com.eb3.ddt;

import?org.apache.commons.lang.StringUtils;

public?class?DBHelper {

private?static?ThreadLocal dbContext = new?ThreadLocal();

//?寫數(shù)據(jù)源標(biāo)識(shí)

public?final?static?String DB_WRITE = "dataSourceWrite";

//?讀數(shù)據(jù)源標(biāo)識(shí)

public?final?static?String DB_READ = "dataSourceRead";

/**

*?獲取數(shù)據(jù)源類型,即是寫數(shù)據(jù)源,還是讀數(shù)據(jù)源

*

* @return

*/

public?static?String getDbType() {

String db_type = dbContext.get();

if?(StringUtils.isEmpty(db_type)) {

//?默認(rèn)是寫數(shù)據(jù)源

db_type = DB_WRITE;

}

return?db_type;

}

/**

*?設(shè)置該線程的數(shù)據(jù)源類型

*

* @param?str

*/

public?static?void?setDbType(String str) {

dbContext.set(str);

}

}

五、服務(wù)層調(diào)用

/*@Aspect?此注解會(huì)影響數(shù)據(jù)源切換,運(yùn)行代碼得知不加的話會(huì)先執(zhí)行DynamicDataSource里的determineCurrentLookupKey方法,后執(zhí)行Service層里DBHelper.setDbType()方法,導(dǎo)致數(shù)據(jù)源切換失敗!*/

@Aspect

@Component("userService")public?class?UserServiceImpl extends?BaseServiceImpl implements?UserService {

@Resource

private?UserDao userDao;

@Override

protected?BaseDao getDao() {

return?this.userDao;

}

@Override

public?void?save(User user) {

DBHelper.setDbType(DBHelper.DB_WRITE); //?寫庫(kù)?(向數(shù)據(jù)庫(kù)中寫)

this.userDao.save(user);

}

@Override

public?User findByUserName(String username) {

DBHelper.setDbType(DBHelper.DB_READ); //?讀庫(kù)?(從數(shù)據(jù)庫(kù)中向外讀)

List userList = this.userDao.findBy("username", username);

return?CollectionUtils.isNotEmpty(userList) ? userList.get(0) : null;

}

}

總結(jié)

以上是生活随笔為你收集整理的java dbtype_Java实现数据库的读写分离的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。