- 浏览: 733893 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
梦行Monxin商城系统:
java网上商城与php网上商城比较 -
梦行Monxin商城系统:
java网上商城与php网上商城比较 -
任楚娴:
你好,请问html = nvl(html); 这句中的nvl( ...
java html串转换成文本串 -
u013246812:
,谢拉!
jQuery ui Dialog 讲解参数 -
大宝剑99:
...
前端优化
思想 就是 代理 DataSource对象 代理 getConnection方法
在代理connection对象 代理 close方法
/**
*
*/
package com.mjp.core.db.jdbc.ds;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Vector;
import javax.sql.DataSource;
import com.mjp.core.db.tool.DbUtils;
import com.mjp.core.properties.PropertiesFacade;
/**
* @author Administrator
*
*/
public class DbcpDataSource implements DataSource {
private static Vector<Connection> connPool;
private static int poolMaxSize = 10;
private static int poolMinSize = 1;
private String userName;
private String password;
private String driverClass;
private String url;
public DbcpDataSource() {
init();
}
/**
*
*/
private void init() {
this.url = PropertiesFacade.getProperties("database.url");
this.driverClass = PropertiesFacade.getProperties("database.driver");
this.userName = PropertiesFacade.getProperties("database.user");
this.password = PropertiesFacade.getProperties("database.password");
try {
poolMaxSize = Integer.parseInt(PropertiesFacade
.getProperties("database.maxsize"));
} catch (Exception ex) {
poolMaxSize = 10;
}
try {
poolMinSize = Integer.parseInt(PropertiesFacade
.getProperties("database.minsize"));
} catch (Exception ex) {
poolMinSize = 1;
}
System.out.println("url:" + this.url);
System.out.println("driverClass:" + this.driverClass);
System.out.println("userName:" + this.userName);
System.out.println("password:" + this.password);
connPool = new Vector<Connection>();
for (int i = 0; i < poolMaxSize; i++) {
connPool.add(createConnection());
}
}
/**
* @return
*/
private Connection createConnection() {
Connection connection = null;
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
connection = DriverManager.getConnection(url, userName, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
private Connection createProxyConnection(Connection connection){
if(connection == null){
connection = createConnection();
}
ProxyConnectionHandler proxyHandler = new ProxyConnectionHandler();
proxyHandler.setConnection(connection);
return proxyHandler.proxyBind();
}
/*
* (non-Javadoc)
*
* @see javax.sql.DataSource#getConnection()
*/
public Connection getConnection() {
int size = connPool.size();
if (connPool.size() < poolMinSize) {
return createProxyConnection(null);
} else {
Connection connection = (Connection) connPool.get(size - 1);
connPool.remove(size - 1);
return createProxyConnection(connection);
}
}
public static synchronized void releaseConnection(Connection connection) {
if(connPool.size() > poolMaxSize){
DbUtils.closeQuietly(connection);
}else{
connPool.add(connection);
}
}
/*
* (non-Javadoc)
*
* @see javax.sql.DataSource#getConnection(java.lang.String,
* java.lang.String)
*/
public Connection getConnection(String arg0, String arg1)
throws SQLException {
return null;
}
/*
* (non-Javadoc)
*
* @see javax.sql.DataSource#getLogWriter()
*/
public PrintWriter getLogWriter() throws SQLException {
return null;
}
/*
* (non-Javadoc)
*
* @see javax.sql.DataSource#getLoginTimeout()
*/
public int getLoginTimeout() throws SQLException {
return 0;
}
/*
* (non-Javadoc)
*
* @see javax.sql.DataSource#setLogWriter(java.io.PrintWriter)
*/
public void setLogWriter(PrintWriter arg0) throws SQLException {
}
/*
* (non-Javadoc)
*
* @see javax.sql.DataSource#setLoginTimeout(int)
*/
public void setLoginTimeout(int arg0) throws SQLException {
}
}
=========================
package com.mjp.core.db.jdbc.ds;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import com.mjp.core.util.ProxyUtils;
public class ProxyConnectionHandler implements InvocationHandler {
private Connection connection;
public ProxyConnectionHandler(){}
public ProxyConnectionHandler(Connection connection) {
this.connection = connection;
}
/* (non-Javadoc)
* @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
*/
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if (method.getName().equals("close")) {
if(connection.getAutoCommit() == false){
try{
connection.commit();
}catch(Exception ex){
connection.rollback();
ex.printStackTrace();
}finally{
try{
connection.setAutoCommit(true);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
DbcpDataSource.releaseConnection(connection);
return null;
} else {
return method.invoke(connection, args);
}
}
public Connection proxyBind() {
Connection proxyConnection = (Connection) Proxy.newProxyInstance(
connection.getClass().getClassLoader(), ProxyUtils.getAllIntefaces(connection.getClass())
, this);
return proxyConnection;
}
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
}
在代理connection对象 代理 close方法
/**
*
*/
package com.mjp.core.db.jdbc.ds;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Vector;
import javax.sql.DataSource;
import com.mjp.core.db.tool.DbUtils;
import com.mjp.core.properties.PropertiesFacade;
/**
* @author Administrator
*
*/
public class DbcpDataSource implements DataSource {
private static Vector<Connection> connPool;
private static int poolMaxSize = 10;
private static int poolMinSize = 1;
private String userName;
private String password;
private String driverClass;
private String url;
public DbcpDataSource() {
init();
}
/**
*
*/
private void init() {
this.url = PropertiesFacade.getProperties("database.url");
this.driverClass = PropertiesFacade.getProperties("database.driver");
this.userName = PropertiesFacade.getProperties("database.user");
this.password = PropertiesFacade.getProperties("database.password");
try {
poolMaxSize = Integer.parseInt(PropertiesFacade
.getProperties("database.maxsize"));
} catch (Exception ex) {
poolMaxSize = 10;
}
try {
poolMinSize = Integer.parseInt(PropertiesFacade
.getProperties("database.minsize"));
} catch (Exception ex) {
poolMinSize = 1;
}
System.out.println("url:" + this.url);
System.out.println("driverClass:" + this.driverClass);
System.out.println("userName:" + this.userName);
System.out.println("password:" + this.password);
connPool = new Vector<Connection>();
for (int i = 0; i < poolMaxSize; i++) {
connPool.add(createConnection());
}
}
/**
* @return
*/
private Connection createConnection() {
Connection connection = null;
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
connection = DriverManager.getConnection(url, userName, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
private Connection createProxyConnection(Connection connection){
if(connection == null){
connection = createConnection();
}
ProxyConnectionHandler proxyHandler = new ProxyConnectionHandler();
proxyHandler.setConnection(connection);
return proxyHandler.proxyBind();
}
/*
* (non-Javadoc)
*
* @see javax.sql.DataSource#getConnection()
*/
public Connection getConnection() {
int size = connPool.size();
if (connPool.size() < poolMinSize) {
return createProxyConnection(null);
} else {
Connection connection = (Connection) connPool.get(size - 1);
connPool.remove(size - 1);
return createProxyConnection(connection);
}
}
public static synchronized void releaseConnection(Connection connection) {
if(connPool.size() > poolMaxSize){
DbUtils.closeQuietly(connection);
}else{
connPool.add(connection);
}
}
/*
* (non-Javadoc)
*
* @see javax.sql.DataSource#getConnection(java.lang.String,
* java.lang.String)
*/
public Connection getConnection(String arg0, String arg1)
throws SQLException {
return null;
}
/*
* (non-Javadoc)
*
* @see javax.sql.DataSource#getLogWriter()
*/
public PrintWriter getLogWriter() throws SQLException {
return null;
}
/*
* (non-Javadoc)
*
* @see javax.sql.DataSource#getLoginTimeout()
*/
public int getLoginTimeout() throws SQLException {
return 0;
}
/*
* (non-Javadoc)
*
* @see javax.sql.DataSource#setLogWriter(java.io.PrintWriter)
*/
public void setLogWriter(PrintWriter arg0) throws SQLException {
}
/*
* (non-Javadoc)
*
* @see javax.sql.DataSource#setLoginTimeout(int)
*/
public void setLoginTimeout(int arg0) throws SQLException {
}
}
=========================
package com.mjp.core.db.jdbc.ds;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import com.mjp.core.util.ProxyUtils;
public class ProxyConnectionHandler implements InvocationHandler {
private Connection connection;
public ProxyConnectionHandler(){}
public ProxyConnectionHandler(Connection connection) {
this.connection = connection;
}
/* (non-Javadoc)
* @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
*/
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if (method.getName().equals("close")) {
if(connection.getAutoCommit() == false){
try{
connection.commit();
}catch(Exception ex){
connection.rollback();
ex.printStackTrace();
}finally{
try{
connection.setAutoCommit(true);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
DbcpDataSource.releaseConnection(connection);
return null;
} else {
return method.invoke(connection, args);
}
}
public Connection proxyBind() {
Connection proxyConnection = (Connection) Proxy.newProxyInstance(
connection.getClass().getClassLoader(), ProxyUtils.getAllIntefaces(connection.getClass())
, this);
return proxyConnection;
}
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
}
发表评论
-
全国最新行政区划代码(Tree)json数据以及地理数据
2020-11-11 21:23 339基于阿里提供的地理数据 http://datav.aliyun ... -
小知识点--解锁 AppChecker ping ip
2019-06-19 08:40 394在使用AppChecker 进行主动拨测的时候,目 ... -
记录年会小系统
2018-02-04 14:42 1121有点意思,年会过后,抽奖活动saas小系统扒光看看:支持P ... -
jdbc方式以 sys账号连接oracle数据的问题
2017-08-22 10:44 452多年以后,在操作oracle 时 发现 jdbc 连 ... -
proguard 代码混淆
2016-11-22 10:59 667url:http://proguard.sourceforg ... -
solr 表达式意义
2015-02-03 19:29 7441. “:”指定字段查指定值,如返回所有值*:* 2. “? ... -
solr 安装配置
2015-02-03 13:15 717solr 安装配置步骤: 1 首先下载solr(最新 ... -
javax.net.ssl.SSLKeyException: RSA premaster secre临时解决方案
2014-11-25 19:38 4367javax.net.ssl.SSLKeyException ... -
compass 小知识点
2014-09-15 10:22 904今天发现排序不管事, 查了一下:索引的问题:排序的列不进 ... -
java_opts 常用
2014-08-14 13:16 1166JVM设置,可以查考如下配置example:# Memory ... -
RPC Socket 服务调用模型
2014-04-09 16:14 574最近在看Dubbo 服务框架, 通过RPC 实现了输入和 ... -
JVM 基本参数
2014-04-03 11:49 761基本的JVM参数:-classpath, -cp: 设置类搜 ... -
eclipse 快捷键失效解决方法
2014-01-17 17:20 694有时候eclispe快捷键失效了,编码起来太难受了。 ... -
Compass+ lucene 通用代码(脱离数据库)
2013-11-08 15:09 866最近需要通过检索分类的方式进行处理,所以就写了接口与实 ... -
DEBUG远程tomcat
2013-08-21 13:35 971一、在windows系统中:打开%CATALINE_H ... -
自动获取网页内容,然后写入excel文档中
2013-07-05 15:22 3418今天需要将北京物业公司名称以及电话给整理出来,发现爱帮 ... -
继续学习jersey+spring 构建RESTful风格 webservice
2013-03-13 10:36 3419j先说一下springmvc rest风格 spring ... -
尽量少使用jaxb 进行xml与对象互换
2013-03-09 17:33 1303尽量少使用jaxb 进行xml与对象互换 问题不少,这个好 ... -
Cookie path的设置
2013-02-21 10:58 1914cookie path 设置 IE对如下的co ... -
http 相关知识点
2012-10-11 10:49 1022Host头域 Host头域指定请求资源的Intenet主机和 ...
相关推荐
Proxy 模式学习代码,包括静态代理,动态代理,cglib等
到工厂方法到抽象工厂,这几种都带有“工厂”的模式,总是容易叫人迷糊,我仔细研究了下,然后用简单的例子做类比,列出了自己的通俗理解和比较,大部分是自己的体会,感觉理的比较清楚,末尾提了下Proxy模式。
设计模式C++学习之代理模式(Proxy)
数据源代理 关于 为JDBC API提供代理类,以拦截正在执行的查询和方法。 版本号 2.x (开发中) ... < artifactId>datasource-proxy [LATEST_VERSION] 最新版本是: 不依赖其他库,一切都是可选的。
23种设计模式之十二(结构型模式)Proxy模式
代码仅供参考学习 。
JAVA Proxy 代理模式
用于分布式跟踪的库(如果在classpath中找到)可启用jdbc连接和查询跟踪(仅适用于p6spy或datasource-proxy) 为什么不将DataSource包装在配置中? 除了使用库之外,您还可以手动包装DataSource ,但是该库还提供了...
利用wsdl.exe生成webservice代理类: 根据提供的wsdl生成webservice代理类 1、开始->程序->Visual Studio 2005 命令提示 2、输入如下红色标记部分 D:\Program Files\Microsoft Visual Studio 8\VC>wsdl /...
C#面向对象设计模式纵横谈(13):Proxy 代理模式(结构型模式)
MySQL数据库中间件:基于C语言开发,包含59个文件,包括22个C源文件...该项目是一个分布式MySQL数据库中间件,采用Proxy模式设计,基于核心业务对象切分,旨在提供高效的数据库查询和处理能力,适用于分布式系统环境。
C#面向对象设计模式纵横谈(13):Proxy 代理模式(结构型模式) (Level 300)
java.proxy,代理模式源码,设计模式,apache开源项目源码commons-proxy-1.0-src 各种代理模式操作的工具类源码以及代理模式案例源码,你会从中得到意想不到的效果! apache开源组织开发的开源项目源码,其优良的代码...
Proxy代理模式的经典实例(很有面向对象思想哦)。快速学会Proxy代理模式.
代理模式java代码 Proxy(4) 开发宝典......
ROS透明代理(Web proxy)加快网页浏览速度详细版,包括WEB proxy的建立、安全设置及利用web proxy所进行上网管理行为的设定等。
Proxy Pattern 代理模式 采用JAVA实现,可以下载看看。
1. 打开Chrome -> 自定义及控制按钮(右上角) -> 更多工具 -> 扩展程序 (有可能需要打开开发者模式) 2. 拖拽Proxy-SwitchyOmega_2_5_15_0.crx到扩展管理界面 3. 安装 原始安装路径: ...
proxy源代码,linux下的ftp 代理的源代码,大家多多支持啊