- 浏览: 678528 次
文章分类
最新评论
数据源
package edu.sdkd.datasource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.Properties;
import edu.sdkd.utils.Utils;
public class MyDataSource {
private static Properties properties = Utils.readProperties("db.properties");
private static String driver = properties.getProperty("driver");
private static String url = properties.getProperty("url");
private static String user = properties.getProperty("user");
private static String password = properties.getProperty("password");
private static int initCount = Integer.valueOf(properties.getProperty("initCount"));
private static int maxCount = Integer.valueOf(properties.getProperty("maxCount"));
private static int currentCount = Integer.valueOf(properties.getProperty("currentCount"));
LinkedList<Connection> connectionsPool = new LinkedList<Connection>();
{
for (int i = 0; i < initCount; i++) {
try {
this.connectionsPool.addLast(this.createConnection());
this.currentCount++;
} catch (SQLException e) {
throw new ExceptionInInitializerError();
}
}
}
static{
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
new RuntimeException("加载驱动失败");
}
}
private static final MyDataSource myDataSource = new MyDataSource();
private MyDataSource() {
}
public static MyDataSource getMyDataSource() {
return myDataSource;
}
public Connection getConnection() throws SQLException {
synchronized (connectionsPool) {
if (this.connectionsPool.size() > 0)
return this.connectionsPool.removeFirst();
if (this.currentCount < this.maxCount) {
this.currentCount++;
return this.createConnection();
}
throw new SQLException("已没有连接");
}
}
public void free(Connection conn) {
this.connectionsPool.addLast(conn);
}
private Connection createConnection() throws SQLException {
Connection realConn = DriverManager.getConnection(url, user, password);
MyConnectionHandler proxy = new MyConnectionHandler(this);
return proxy.bind(realConn);
}
}
--------------------------------------------------------------------------
package edu.sdkd.datasource;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
class MyConnectionHandler implements InvocationHandler {
private Connection realConnection;
private Connection warpedConnection;
private MyDataSource dataSource;
MyConnectionHandler(MyDataSource dataSource) {
this.dataSource = dataSource;
}
Connection bind(Connection realConn) {
this.realConnection = realConn;
this.warpedConnection = (Connection)Proxy.newProxyInstance(this.getClass().getClassLoader(),
new Class[] { Connection.class }, this);
return warpedConnection;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if("close".equals(method.getName())){
this.dataSource.connectionsPool.addLast(this.warpedConnection);
}
return method.invoke(this.realConnection, args);
}
}
-------------------------------------------------------------------------------------------------------------
package edu.sdkd.datasource;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
public class MyConnection implements Connection{
private Connection realConnection;
private MyDataSource dataSource;
MyConnection(Connection realConnection, MyDataSource dataSource){
this.realConnection = realConnection;
this.dataSource = dataSource;
}
public void clearWarnings() throws SQLException {
this.realConnection.clearWarnings();
}
public void close() throws SQLException {
this.dataSource.connectionsPool.addLast(this);
}
public void commit() throws SQLException {
this.realConnection.commit();
}
public Array createArrayOf(String typeName, Object[] elements)
throws SQLException {
return this.realConnection.createArrayOf(typeName, elements);
}
public Blob createBlob() throws SQLException {
return this.realConnection.createBlob();
}
public Clob createClob() throws SQLException {
return this.realConnection.createClob();
}
public NClob createNClob() throws SQLException {
return this.realConnection.createNClob();
}
public SQLXML createSQLXML() throws SQLException {
return this.realConnection.createSQLXML();
}
public Statement createStatement() throws SQLException {
return this.realConnection.createStatement();
}
public Statement createStatement(int resultSetType, int resultSetConcurrency)
throws SQLException {
return this.realConnection.createStatement();
}
public void abort(Executor executor) throws SQLException {
// TODO Auto-generated method stub
}
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public boolean getAutoCommit() throws SQLException {
// TODO Auto-generated method stub
return false;
}
public String getCatalog() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public Properties getClientInfo() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public String getClientInfo(String name) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public int getHoldability() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public DatabaseMetaData getMetaData() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public int getNetworkTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public String getSchema() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public int getTransactionIsolation() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
public Map<String, Class<?>> getTypeMap() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public SQLWarning getWarnings() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public boolean isClosed() throws SQLException {
// TODO Auto-generated method stub
return false;
}
public boolean isReadOnly() throws SQLException {
// TODO Auto-generated method stub
return false;
}
public boolean isValid(int timeout) throws SQLException {
// TODO Auto-generated method stub
return false;
}
public String nativeSQL(String sql) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public CallableStatement prepareCall(String sql) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public PreparedStatement prepareStatement(String sql) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
// TODO Auto-generated method stub
}
public void rollback() throws SQLException {
// TODO Auto-generated method stub
}
public void rollback(Savepoint savepoint) throws SQLException {
// TODO Auto-generated method stub
}
public void setAutoCommit(boolean autoCommit) throws SQLException {
// TODO Auto-generated method stub
}
public void setCatalog(String catalog) throws SQLException {
// TODO Auto-generated method stub
}
public void setClientInfo(Properties properties) throws SQLClientInfoException {
// TODO Auto-generated method stub
}
public void setClientInfo(String name, String value) throws SQLClientInfoException {
// TODO Auto-generated method stub
}
public void setHoldability(int holdability) throws SQLException {
// TODO Auto-generated method stub
}
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
// TODO Auto-generated method stub
}
public void setReadOnly(boolean readOnly) throws SQLException {
// TODO Auto-generated method stub
}
public Savepoint setSavepoint() throws SQLException {
// TODO Auto-generated method stub
return null;
}
public Savepoint setSavepoint(String name) throws SQLException {
// TODO Auto-generated method stub
return null;
}
public void setSchema(String schema) throws SQLException {
// TODO Auto-generated method stub
}
public void setTransactionIsolation(int level) throws SQLException {
// TODO Auto-generated method stub
}
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
// TODO Auto-generated method stub
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// TODO Auto-generated method stub
return false;
}
public <T> T unwrap(Class<T> iface) throws SQLException {
// TODO Auto-generated method stub
return null;
}
}
-------------------------------------------------------------------------------------------
相关推荐
1. 基于Aspectj实现动态数据源切换,支持类级、方法级,方法级优先于类级 2. 实现数据源动态注册、修改、删除,无需重启服务 3. 实现同时兼容多种数据源连接池 4. 实现动态数据源连接池属性配置 5. 采用双端队列实现...
整理的分布式事务 及动态数据源切换 基于数据库XA协议 maven 包可直接使用 maven 引入路径: <groupId>com.maple</groupId> <artifactId>DCSDataSourcesServer <version>0.0.1-SNAPSHOT 事务及数据源标签...
通过简单的demo实现SpingBoot多数据源配置并动态切换多数据源
Spark SQL的DataFrame接口支持多种数据源的操作。一个DataFrame可以进行RDDs方式的操作,也可以被注册为临时表。把DataFrame注册为临时表之后,就可以对该DataFrame执行SQL查询。 Spark SQL的默认数据源为Parquet...
ssm实现多数据源配置ssm实现多数据源配置ssm实现多数据源配置ssm实现多数据源配置,
异质的多数据源,是指一个系统可以同 时支持两种及上不类型库比如异质的多数据源,是指一个系统可以同 时支持两种及上不类型库比如异质的多数据源,是指一个系统可以同 时支持两种及上不类型库比如异质的多数据源,...
java多数据源—源码(支持sqlserver,mysql,oracle等等);解压后,直接使用idea导入项目,默认是打开了多数据源的,项目里面有现成的sql文件,执行sql文件;多数据源的开关在application-local.yml文件里面的guns:...
Visual C++源代码 150 如何以数据源方式导出Excel文件Visual C++源代码 150 如何以数据源方式导出Excel文件Visual C++源代码 150 如何以数据源方式导出Excel文件Visual C++源代码 150 如何以数据源方式导出Excel文件...
weblogic数据源配置 weblogic数据源配置 weblogic数据源配置
MyEclipse配置数据源
mybatis-plus+动态数据源+自定义数据源配置+dom解析+自定义参数注入(注解)+AOP切换数据源+druid
springboot + mybatis-plus + database+ 多数据源 + redis + hutool 框架干净,没有其他冗余的成分; 配置了MP的代码生成器,意见生成代码,节省开发时间! 可用于各种定时任务处理,各种夸库操作, 多数据源支持...
spring boot 2多数据源,里面有hibernate和mybatis的多数据源代码。代码拿过去即可集成使用。
spring 配置多数据源
最近项目中的工作流需要查询多个数据源的数据,数据源可能是不同种类的:如sql server,oracl等等,一开始是用的配置实现,后来发现在项目运行中,可能需要动态的添加更多不同类型的数据源,所以最终的逻辑是将数据源...
BW增强数据源的两种方法, 增强数据源的两种方式方法介绍
基于注解的Spring多数据源配置和使用 前一段时间研究了一下spring多数据源的配置和使用,为了后期从多个数据源拉取数据定时进行数据分析和报表统计做准备。由于之前做过的项目都是单数据源的,没有遇到这种场景,...
项目背景: 1. Springboot项目 ... 一个主数据源,数据名称库不变。 3. 一个副数据源,数据库名称每天都在变,比如今天1号,是db_01; 明天2号,是db_02。 4.不重启项目切换数据源。 5.项目介绍: ...
最近在开发一个项目中遇到一个odbc数据源问题。 问题描述: 目前开发的项目采用IBM的domino与DB2集成的技术。由于新版本的升级与旧版本的技术体系不同,数据结构不一样。 为了很好的过渡新旧版本系统...
定义ODBC数据源 定义ODBC数据源 定义ODBC数据源