- 浏览: 91717 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
guoyunsky:
不错,但ubuntu下只要一行命令即可:sudo aptitu ...
ubuntu 安装 Pidgin -
komei:
#连接设置jdbc.driverClassName=oracl ...
derby dbcp -
wsbjwjt:
我要试一下
window subversion -
komei:
### This file is an example aut ...
SVN -
komei:
http://jhcore.com/2007/06/04/in ...
ubuntu 安装 Pidgin
import java.util.concurrent.Semaphore;
import java.util.Stack;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.TimeUnit;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.ConnectionEvent;
import javax.sql.ConnectionEventListener;
import javax.sql.PooledConnection;
/**
* A simple standalone JDBC connection pool manager.
* <p>
* The public methods of this class are thread-safe.
* <p>
* Author: Christian d'Heureuse (<a
* href="http://www.source-code.biz">www.source-code.biz</a>)<br>
* License: <a href="http://www.gnu.org/licenses/lgpl.html">LGPL</a>.
* <p>
* 2007-06-21: Constructor with a timeout parameter added.
*/
public class MiniConnectionPoolManager {
private ConnectionPoolDataSource dataSource;
private int maxConnections;
private int timeout;
private PrintWriter logWriter;
private Semaphore semaphore;
private Stack<PooledConnection> recycledConnections;
private int activeConnections;
private PoolConnectionEventListener poolConnectionEventListener;
private boolean isDisposed;
/**
* Thrown in {@link #getConnection()} when no free connection becomes available
* within <code>timeout</code> seconds.
*/
public static class TimeoutException extends RuntimeException {
private static final long serialVersionUID = 1;
public TimeoutException () {
super ("Timeout while waiting for a free database connection."); }}
/**
* Constructs a MiniConnectionPoolManager object with a timeout of 60 seconds.
*
* @param dataSource
* the data source for the connections.
* @param maxConnections
* the maximum number of connections.
*/
public MiniConnectionPoolManager (ConnectionPoolDataSource dataSource, int maxConnections) {
this (dataSource, maxConnections, 60); }
/**
* Constructs a MiniConnectionPoolManager object.
*
* @param dataSource
* the data source for the connections.
* @param maxConnections
* the maximum number of connections.
* @param timeout
* the maximum time in seconds to wait for a free connection.
*/
public MiniConnectionPoolManager (ConnectionPoolDataSource dataSource, int maxConnections, int timeout) {
this.dataSource = dataSource;
this.maxConnections = maxConnections;
this.timeout = timeout;
try {
logWriter = dataSource.getLogWriter(); }
catch (SQLException e) {}
if (maxConnections < 1) throw new IllegalArgumentException("Invalid maxConnections value.");
semaphore = new Semaphore(maxConnections,true);
recycledConnections = new Stack<PooledConnection>();
poolConnectionEventListener = new PoolConnectionEventListener(); }
/**
* Closes all unused pooled connections.
*/
public synchronized void dispose() throws SQLException {
if (isDisposed) return;
isDisposed = true;
SQLException e = null;
while (!recycledConnections.isEmpty()) {
PooledConnection pconn = recycledConnections.pop();
try {
pconn.close(); }
catch (SQLException e2) {
if (e == null) e = e2; }}
if (e != null) throw e; }
/**
* Retrieves a connection from the connection pool. If
* <code>maxConnections</code> connections are already in use, the method
* waits until a connection becomes available or <code>timeout</code> seconds
* elapsed. When the application is finished using the connection, it must close
* it in order to return it to the pool.
*
* @return a new Connection object.
* @throws TimeoutException
* when no connection becomes available within <code>timeout</code>
* seconds.
*/
public Connection getConnection() throws SQLException {
// This routine is unsynchronized, because semaphore.acquire() may block.
synchronized (this) {
if (isDisposed) throw new IllegalStateException("Connection pool has been disposed."); }
try {
if (!semaphore.tryAcquire(timeout,TimeUnit.SECONDS))
throw new TimeoutException(); }
catch (InterruptedException e) {
throw new RuntimeException("Interrupted while waiting for a database connection.",e); }
boolean ok = false;
try {
Connection conn = getConnection2();
ok = true;
return conn; }
finally {
if (!ok) semaphore.release(); }}
private synchronized Connection getConnection2() throws SQLException {
if (isDisposed) throw new IllegalStateException("Connection pool has been disposed."); // test
// again
// with
// lock
PooledConnection pconn;
if (!recycledConnections.empty()) {
pconn = recycledConnections.pop(); }
else {
pconn = dataSource.getPooledConnection(); }
Connection conn = pconn.getConnection();
activeConnections++;
pconn.addConnectionEventListener (poolConnectionEventListener);
assertInnerState();
return conn; }
private synchronized void recycleConnection (PooledConnection pconn) {
if (isDisposed) { disposeConnection (pconn); return; }
if (activeConnections <= 0) throw new AssertionError();
activeConnections--;
semaphore.release();
recycledConnections.push (pconn);
assertInnerState(); }
private synchronized void disposeConnection (PooledConnection pconn) {
if (activeConnections <= 0) throw new AssertionError();
activeConnections--;
semaphore.release();
closeConnectionNoEx (pconn);
assertInnerState(); }
private void closeConnectionNoEx (PooledConnection pconn) {
try {
pconn.close(); }
catch (SQLException e) {
log ("Error while closing database connection: "+e.toString()); }}
private void log (String msg) {
String s = "MiniConnectionPoolManager: "+msg;
try {
if (logWriter == null)
System.err.println (s);
else
logWriter.println (s); }
catch (Exception e) {}}
private void assertInnerState() {
if (activeConnections < 0) throw new AssertionError();
if (activeConnections+recycledConnections.size() > maxConnections) throw new AssertionError();
if (activeConnections+semaphore.availablePermits() > maxConnections) throw new AssertionError(); }
private class PoolConnectionEventListener implements ConnectionEventListener {
public void connectionClosed (ConnectionEvent event) {
PooledConnection pconn = (PooledConnection)event.getSource();
pconn.removeConnectionEventListener (this);
recycleConnection (pconn); }
public void connectionErrorOccurred(ConnectionEvent event) {
PooledConnection pconn = (PooledConnection)event.getSource();
pconn.removeConnectionEventListener (this);
disposeConnection (pconn); }}
/**
* Returns the number of active (open) connections of this pool. This is the
* number of <code>Connection</code> objects that have been issued by
* {@link #getConnection()} for which <code>Connection.close()</code> has not
* yet been called.
*
* @return the number of active connections.
*/
public synchronized int getActiveConnections() {
return activeConnections; }
} // end class MiniConnectionPoolManager
// Test program for the MiniConnectionPoolManager class.
import java.io.PrintWriter;
import java.lang.Thread;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
import javax.sql.ConnectionPoolDataSource;
import com.oval.research.connpool.MiniConnectionPoolManager;
public class TestMiniConnectionPoolManager {
private static final int maxConnections = 8; // number of connections
private static final int noOfThreads = 50; // number of worker threads
private static final int processingTime = 30; // total processing time of
// the test program in
// seconds
private static final int threadPauseTime1 = 100; // max. thread pause
// time in microseconds,
// without a connection
private static final int threadPauseTime2 = 100; // max. thread pause
// time in microseconds,
// with a connection
private static MiniConnectionPoolManager poolMgr;
private static WorkerThread[] threads;
private static boolean shutdownFlag;
private static Object shutdownObj = new Object();
private static Random random = new Random();
private static class WorkerThread extends Thread {
public int threadNo;
public void run() {
threadMain(threadNo);
}
};
private static ConnectionPoolDataSource createDataSource() throws Exception {
// Version for H2:
/*
* org.h2.jdbcx.JdbcDataSource dataSource = new
* org.h2.jdbcx.JdbcDataSource(); dataSource.setURL
* ("jdbc:h2:file:c:/temp/temp_TestMiniConnectionPoolManagerDB;DB_CLOSE_DELAY=-1");
*/
// Version for Apache Derby:
org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource dataSource = new org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource();
dataSource
.setDatabaseName("e:/mimiConnection/temp_TestMiniConnectionPoolManagerDB");
dataSource.setCreateDatabase("create");
dataSource.setLogWriter(new PrintWriter(System.out));
// Versioo for JTDS:
/*
* net.sourceforge.jtds.jdbcx.JtdsDataSource dataSource = new
* net.sourceforge.jtds.jdbcx.JtdsDataSource(); dataSource.setAppName
* ("TestMiniConnectionPoolManager"); dataSource.setDatabaseName
* ("Northwind"); dataSource.setServerName ("localhost");
* dataSource.setUser ("sa"); dataSource.setPassword
* (System.getProperty("saPassword"));
*/
// Version for the Microsoft SQL Server driver (sqljdbc.jar):
/*
* // The sqljdbc 1.1 documentation, chapter "Using Connection Pooling",
* recommends to use // SQLServerXADataSource instead of
* SQLServerConnectionPoolDataSource, even when no // distributed
* transactions are used.
* com.microsoft.sqlserver.jdbc.SQLServerXADataSource dataSource = new
* com.microsoft.sqlserver.jdbc.SQLServerXADataSource();
* dataSource.setApplicationName ("TestMiniConnectionPoolManager");
* dataSource.setDatabaseName ("Northwind"); dataSource.setServerName
* ("localhost"); dataSource.setUser ("sa"); dataSource.setPassword
* (System.getProperty("saPassword")); dataSource.setLogWriter (new
* PrintWriter(System.out));
*/
return dataSource;
}
public static void main(String[] args) throws Exception {
System.out.println("Program started.");
ConnectionPoolDataSource dataSource = createDataSource();
poolMgr = new MiniConnectionPoolManager(dataSource, maxConnections);
initDb();
startWorkerThreads();
pause(processingTime * 1000000);
System.out.println("\nStopping threads.");
stopWorkerThreads();
System.out.println("\nAll threads stopped.");
poolMgr.dispose();
System.out.println("Program completed.");
}
private static void startWorkerThreads() {
threads = new WorkerThread[noOfThreads];
for (int threadNo = 0; threadNo < noOfThreads; threadNo++) {
WorkerThread thread = new WorkerThread();
threads[threadNo] = thread;
thread.threadNo = threadNo;
thread.start();
}
}
private static void stopWorkerThreads() throws Exception {
setShutdownFlag();
for (int threadNo = 0; threadNo < noOfThreads; threadNo++) {
threads[threadNo].join();
}
}
private static void setShutdownFlag() {
synchronized (shutdownObj) {
shutdownFlag = true;
shutdownObj.notifyAll();
}
}
private static void threadMain(int threadNo) {
try {
threadMain2(threadNo);
} catch (Throwable e) {
System.out.println("\nException in thread " + threadNo + ": " + e);
e.printStackTrace(System.out);
setShutdownFlag();
}
}
private static void threadMain2(int threadNo) throws Exception {
// System.out.println ("Thread "+threadNo+" started.");
while (true) {
if (!pauseRandom(threadPauseTime1))
return;
threadTask(threadNo);
}
}
private static void threadTask(int threadNo) throws Exception {
Connection conn = null;
try {
conn = poolMgr.getConnection();
if (shutdownFlag)
return;
System.out.print(threadNo + " ");
incrementThreadCounter(conn, threadNo);
pauseRandom(threadPauseTime2);
} finally {
if (conn != null)
conn.close();
}
}
private static boolean pauseRandom(int maxPauseTime) throws Exception {
return pause(random.nextInt(maxPauseTime));
}
private static boolean pause(int pauseTime) throws Exception {
synchronized (shutdownObj) {
if (shutdownFlag)
return false;
if (pauseTime <= 0)
return true;
int ms = pauseTime / 1000;
int ns = (pauseTime % 1000) * 1000;
shutdownObj.wait(ms, ns);
}
return true;
}
private static void initDb() throws SQLException {
Connection conn = null;
try {
conn = poolMgr.getConnection();
System.out.println("initDb connected");
initDb2(conn);
} finally {
if (conn != null)
conn.close();
}
System.out.println("initDb done");
}
private static void initDb2(Connection conn) throws SQLException {
execSqlNoErr(conn, "drop table temp");
execSql(conn, "create table temp (threadNo integer, ctr integer)");
for (int i = 0; i < noOfThreads; i++)
execSql(conn, "insert into temp values(" + i + ",0)");
}
private static void incrementThreadCounter(Connection conn, int threadNo)
throws SQLException {
execSql(conn, "update temp set ctr = ctr + 1 where threadNo="
+ threadNo);
}
private static void execSqlNoErr(Connection conn, String sql) {
try {
execSql(conn, sql);
} catch (SQLException e) {
}
}
private static void execSql(Connection conn, String sql)
throws SQLException {
Statement st = null;
try {
st = conn.createStatement();
st.executeUpdate(sql);
} finally {
if (st != null)
st.close();
}
}
} // end class TestMiniConnectionPoolManager
import java.util.Stack;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.TimeUnit;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.ConnectionEvent;
import javax.sql.ConnectionEventListener;
import javax.sql.PooledConnection;
/**
* A simple standalone JDBC connection pool manager.
* <p>
* The public methods of this class are thread-safe.
* <p>
* Author: Christian d'Heureuse (<a
* href="http://www.source-code.biz">www.source-code.biz</a>)<br>
* License: <a href="http://www.gnu.org/licenses/lgpl.html">LGPL</a>.
* <p>
* 2007-06-21: Constructor with a timeout parameter added.
*/
public class MiniConnectionPoolManager {
private ConnectionPoolDataSource dataSource;
private int maxConnections;
private int timeout;
private PrintWriter logWriter;
private Semaphore semaphore;
private Stack<PooledConnection> recycledConnections;
private int activeConnections;
private PoolConnectionEventListener poolConnectionEventListener;
private boolean isDisposed;
/**
* Thrown in {@link #getConnection()} when no free connection becomes available
* within <code>timeout</code> seconds.
*/
public static class TimeoutException extends RuntimeException {
private static final long serialVersionUID = 1;
public TimeoutException () {
super ("Timeout while waiting for a free database connection."); }}
/**
* Constructs a MiniConnectionPoolManager object with a timeout of 60 seconds.
*
* @param dataSource
* the data source for the connections.
* @param maxConnections
* the maximum number of connections.
*/
public MiniConnectionPoolManager (ConnectionPoolDataSource dataSource, int maxConnections) {
this (dataSource, maxConnections, 60); }
/**
* Constructs a MiniConnectionPoolManager object.
*
* @param dataSource
* the data source for the connections.
* @param maxConnections
* the maximum number of connections.
* @param timeout
* the maximum time in seconds to wait for a free connection.
*/
public MiniConnectionPoolManager (ConnectionPoolDataSource dataSource, int maxConnections, int timeout) {
this.dataSource = dataSource;
this.maxConnections = maxConnections;
this.timeout = timeout;
try {
logWriter = dataSource.getLogWriter(); }
catch (SQLException e) {}
if (maxConnections < 1) throw new IllegalArgumentException("Invalid maxConnections value.");
semaphore = new Semaphore(maxConnections,true);
recycledConnections = new Stack<PooledConnection>();
poolConnectionEventListener = new PoolConnectionEventListener(); }
/**
* Closes all unused pooled connections.
*/
public synchronized void dispose() throws SQLException {
if (isDisposed) return;
isDisposed = true;
SQLException e = null;
while (!recycledConnections.isEmpty()) {
PooledConnection pconn = recycledConnections.pop();
try {
pconn.close(); }
catch (SQLException e2) {
if (e == null) e = e2; }}
if (e != null) throw e; }
/**
* Retrieves a connection from the connection pool. If
* <code>maxConnections</code> connections are already in use, the method
* waits until a connection becomes available or <code>timeout</code> seconds
* elapsed. When the application is finished using the connection, it must close
* it in order to return it to the pool.
*
* @return a new Connection object.
* @throws TimeoutException
* when no connection becomes available within <code>timeout</code>
* seconds.
*/
public Connection getConnection() throws SQLException {
// This routine is unsynchronized, because semaphore.acquire() may block.
synchronized (this) {
if (isDisposed) throw new IllegalStateException("Connection pool has been disposed."); }
try {
if (!semaphore.tryAcquire(timeout,TimeUnit.SECONDS))
throw new TimeoutException(); }
catch (InterruptedException e) {
throw new RuntimeException("Interrupted while waiting for a database connection.",e); }
boolean ok = false;
try {
Connection conn = getConnection2();
ok = true;
return conn; }
finally {
if (!ok) semaphore.release(); }}
private synchronized Connection getConnection2() throws SQLException {
if (isDisposed) throw new IllegalStateException("Connection pool has been disposed."); // test
// again
// with
// lock
PooledConnection pconn;
if (!recycledConnections.empty()) {
pconn = recycledConnections.pop(); }
else {
pconn = dataSource.getPooledConnection(); }
Connection conn = pconn.getConnection();
activeConnections++;
pconn.addConnectionEventListener (poolConnectionEventListener);
assertInnerState();
return conn; }
private synchronized void recycleConnection (PooledConnection pconn) {
if (isDisposed) { disposeConnection (pconn); return; }
if (activeConnections <= 0) throw new AssertionError();
activeConnections--;
semaphore.release();
recycledConnections.push (pconn);
assertInnerState(); }
private synchronized void disposeConnection (PooledConnection pconn) {
if (activeConnections <= 0) throw new AssertionError();
activeConnections--;
semaphore.release();
closeConnectionNoEx (pconn);
assertInnerState(); }
private void closeConnectionNoEx (PooledConnection pconn) {
try {
pconn.close(); }
catch (SQLException e) {
log ("Error while closing database connection: "+e.toString()); }}
private void log (String msg) {
String s = "MiniConnectionPoolManager: "+msg;
try {
if (logWriter == null)
System.err.println (s);
else
logWriter.println (s); }
catch (Exception e) {}}
private void assertInnerState() {
if (activeConnections < 0) throw new AssertionError();
if (activeConnections+recycledConnections.size() > maxConnections) throw new AssertionError();
if (activeConnections+semaphore.availablePermits() > maxConnections) throw new AssertionError(); }
private class PoolConnectionEventListener implements ConnectionEventListener {
public void connectionClosed (ConnectionEvent event) {
PooledConnection pconn = (PooledConnection)event.getSource();
pconn.removeConnectionEventListener (this);
recycleConnection (pconn); }
public void connectionErrorOccurred(ConnectionEvent event) {
PooledConnection pconn = (PooledConnection)event.getSource();
pconn.removeConnectionEventListener (this);
disposeConnection (pconn); }}
/**
* Returns the number of active (open) connections of this pool. This is the
* number of <code>Connection</code> objects that have been issued by
* {@link #getConnection()} for which <code>Connection.close()</code> has not
* yet been called.
*
* @return the number of active connections.
*/
public synchronized int getActiveConnections() {
return activeConnections; }
} // end class MiniConnectionPoolManager
// Test program for the MiniConnectionPoolManager class.
import java.io.PrintWriter;
import java.lang.Thread;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
import javax.sql.ConnectionPoolDataSource;
import com.oval.research.connpool.MiniConnectionPoolManager;
public class TestMiniConnectionPoolManager {
private static final int maxConnections = 8; // number of connections
private static final int noOfThreads = 50; // number of worker threads
private static final int processingTime = 30; // total processing time of
// the test program in
// seconds
private static final int threadPauseTime1 = 100; // max. thread pause
// time in microseconds,
// without a connection
private static final int threadPauseTime2 = 100; // max. thread pause
// time in microseconds,
// with a connection
private static MiniConnectionPoolManager poolMgr;
private static WorkerThread[] threads;
private static boolean shutdownFlag;
private static Object shutdownObj = new Object();
private static Random random = new Random();
private static class WorkerThread extends Thread {
public int threadNo;
public void run() {
threadMain(threadNo);
}
};
private static ConnectionPoolDataSource createDataSource() throws Exception {
// Version for H2:
/*
* org.h2.jdbcx.JdbcDataSource dataSource = new
* org.h2.jdbcx.JdbcDataSource(); dataSource.setURL
* ("jdbc:h2:file:c:/temp/temp_TestMiniConnectionPoolManagerDB;DB_CLOSE_DELAY=-1");
*/
// Version for Apache Derby:
org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource dataSource = new org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource();
dataSource
.setDatabaseName("e:/mimiConnection/temp_TestMiniConnectionPoolManagerDB");
dataSource.setCreateDatabase("create");
dataSource.setLogWriter(new PrintWriter(System.out));
// Versioo for JTDS:
/*
* net.sourceforge.jtds.jdbcx.JtdsDataSource dataSource = new
* net.sourceforge.jtds.jdbcx.JtdsDataSource(); dataSource.setAppName
* ("TestMiniConnectionPoolManager"); dataSource.setDatabaseName
* ("Northwind"); dataSource.setServerName ("localhost");
* dataSource.setUser ("sa"); dataSource.setPassword
* (System.getProperty("saPassword"));
*/
// Version for the Microsoft SQL Server driver (sqljdbc.jar):
/*
* // The sqljdbc 1.1 documentation, chapter "Using Connection Pooling",
* recommends to use // SQLServerXADataSource instead of
* SQLServerConnectionPoolDataSource, even when no // distributed
* transactions are used.
* com.microsoft.sqlserver.jdbc.SQLServerXADataSource dataSource = new
* com.microsoft.sqlserver.jdbc.SQLServerXADataSource();
* dataSource.setApplicationName ("TestMiniConnectionPoolManager");
* dataSource.setDatabaseName ("Northwind"); dataSource.setServerName
* ("localhost"); dataSource.setUser ("sa"); dataSource.setPassword
* (System.getProperty("saPassword")); dataSource.setLogWriter (new
* PrintWriter(System.out));
*/
return dataSource;
}
public static void main(String[] args) throws Exception {
System.out.println("Program started.");
ConnectionPoolDataSource dataSource = createDataSource();
poolMgr = new MiniConnectionPoolManager(dataSource, maxConnections);
initDb();
startWorkerThreads();
pause(processingTime * 1000000);
System.out.println("\nStopping threads.");
stopWorkerThreads();
System.out.println("\nAll threads stopped.");
poolMgr.dispose();
System.out.println("Program completed.");
}
private static void startWorkerThreads() {
threads = new WorkerThread[noOfThreads];
for (int threadNo = 0; threadNo < noOfThreads; threadNo++) {
WorkerThread thread = new WorkerThread();
threads[threadNo] = thread;
thread.threadNo = threadNo;
thread.start();
}
}
private static void stopWorkerThreads() throws Exception {
setShutdownFlag();
for (int threadNo = 0; threadNo < noOfThreads; threadNo++) {
threads[threadNo].join();
}
}
private static void setShutdownFlag() {
synchronized (shutdownObj) {
shutdownFlag = true;
shutdownObj.notifyAll();
}
}
private static void threadMain(int threadNo) {
try {
threadMain2(threadNo);
} catch (Throwable e) {
System.out.println("\nException in thread " + threadNo + ": " + e);
e.printStackTrace(System.out);
setShutdownFlag();
}
}
private static void threadMain2(int threadNo) throws Exception {
// System.out.println ("Thread "+threadNo+" started.");
while (true) {
if (!pauseRandom(threadPauseTime1))
return;
threadTask(threadNo);
}
}
private static void threadTask(int threadNo) throws Exception {
Connection conn = null;
try {
conn = poolMgr.getConnection();
if (shutdownFlag)
return;
System.out.print(threadNo + " ");
incrementThreadCounter(conn, threadNo);
pauseRandom(threadPauseTime2);
} finally {
if (conn != null)
conn.close();
}
}
private static boolean pauseRandom(int maxPauseTime) throws Exception {
return pause(random.nextInt(maxPauseTime));
}
private static boolean pause(int pauseTime) throws Exception {
synchronized (shutdownObj) {
if (shutdownFlag)
return false;
if (pauseTime <= 0)
return true;
int ms = pauseTime / 1000;
int ns = (pauseTime % 1000) * 1000;
shutdownObj.wait(ms, ns);
}
return true;
}
private static void initDb() throws SQLException {
Connection conn = null;
try {
conn = poolMgr.getConnection();
System.out.println("initDb connected");
initDb2(conn);
} finally {
if (conn != null)
conn.close();
}
System.out.println("initDb done");
}
private static void initDb2(Connection conn) throws SQLException {
execSqlNoErr(conn, "drop table temp");
execSql(conn, "create table temp (threadNo integer, ctr integer)");
for (int i = 0; i < noOfThreads; i++)
execSql(conn, "insert into temp values(" + i + ",0)");
}
private static void incrementThreadCounter(Connection conn, int threadNo)
throws SQLException {
execSql(conn, "update temp set ctr = ctr + 1 where threadNo="
+ threadNo);
}
private static void execSqlNoErr(Connection conn, String sql) {
try {
execSql(conn, sql);
} catch (SQLException e) {
}
}
private static void execSql(Connection conn, String sql)
throws SQLException {
Statement st = null;
try {
st = conn.createStatement();
st.executeUpdate(sql);
} finally {
if (st != null)
st.close();
}
}
} // end class TestMiniConnectionPoolManager
相关推荐
迷你连接池管理器(MiniConnectionPoolManager)是一个小型且轻量级的数据库连接池实现,专为简化JDBC连接管理而设计。它提供了一种有效的方式,以优化数据库访问性能和资源利用,尤其是在多线程环境中。连接池是...
6. **DbConnectionBroker** 和 **MiniConnectionPoolManager**:这两种轻量级连接池实现相对简单,适用于对性能要求不高的应用场景。 #### 四、连接池的技术特点 1. **API设计**: - DBCP和C3P0因其广泛的使用而...
7. **MiniConnectionPoolManager**:这是Apache Commons DBCP项目的一部分,它是一个简单的连接池管理器,适用于小型应用。它提供了基本的连接池管理功能,如连接初始化、最大连接限制等。 8. **Proxool_0.9.1**:...
9. **MiniConnectionPoolManager**:轻量级且无第三方依赖的JDBC数据库连接池,适用于简单场景。 10. **BoneCP**:快速且开源的连接池,比C3P0和DBCP快25倍,适合性能敏感的应用。 在配置Hibernate与连接池的整合...
三相LCL并网逆变器:高精度快速响应的有功无功解耦控制技术,三相LCL并网逆变器,有功无功解耦控制,控制精度高,响应速度快。 ,三相LCL并网逆变器; 有功无功解耦控制; 高控制精度; 快速响应。,三相LCL逆变器高精度快速响应解耦控制
一种基于Lifelogging视频的文本标签生成模型.pdf
基于黏菌优化算法(SMA)的改进与复现——融合EO算法更新策略的ESMA项目报告,黏菌优化算法(SMA)复现(融合EO算法改进更新策略)——ESMA。 复现内容包括:改进算法实现、23个基准测试函数、多次实验运行并计算均值标准差等统计量、与SMA对比等。 程序基本上每一步都有注释,非常易懂,代码质量极高,便于新手学习和理解。 ,SMA复现;EO算法改进;算法实现;基准测试函数;实验运行;统计量;SMA对比;程序注释;代码质量;学习理解。,标题:ESMA算法复现:黏菌优化与EO算法融合改进的实证研究
免费JAVA毕业设计 2024成品源码+论文+数据库+启动教程 启动教程:https://www.bilibili.com/video/BV1SzbFe7EGZ 项目讲解视频:https://www.bilibili.com/video/BV1Tb421n72S 二次开发教程:https://www.bilibili.com/video/BV18i421i7Dx
基于数据挖掘的教师教育质量评价指标体系的构建.pdf
内容概要:本实验报告旨在介绍将正则表达式(RE)转化为非确定有限自动机(NFA)的过程与技术细节。内容包括了理论背景的介绍,比如为什么需要这样的转换以及它背后的数学逻辑;详细解释如何通过编写特定功能的程序完成从正则表达式到NFA的状态迁移图构建;并且探讨了后续将这个NFA再转变成DFA(确定有限自动机)并进行优化的方法。最后,通过一组具体的例子来进行验证性的实践操作,并讨论在整个过程中遇到的各种挑战及解决方案。此外,报告还包含了对于不同设计方案的选择考量,以及对于所选技术和工具的应用评估。 适用人群:对于希望深入理解编译原理、特别是形式语言和自动机构造的学生或专业人士来说是一份宝贵的学习资料。 使用场景及目标:本篇文章主要用于教育指导,适用于大学本科计算机科学专业相关课程的教学辅助材料,帮助学生更好地理解复杂概念之间的联系。通过动手实践可以强化他们对该领域关键知识点的记忆和技术能力。 其他说明:文中提供的源代码实例和图表有助于使用者更直观地领会转换过程的具体步骤,同时也有助于培养解决问题的能力和思维方式。
nodejs010-1.2-29.el6.centos.alt.x86_64.rpm
"基于萤火虫算法优化麻雀算法的深度置信网络FSSSA-DBN数据分类预测模型及其Matlab代码详解",基于萤火虫算法改进麻雀算法优化深度置信网络(FSSSA-DBN)的数据分类预测 matlab代码注释详细, ,核心关键词:基于萤火虫算法; 改进麻雀算法; 优化深度置信网络(FSSSA-DBN); 数据分类预测; MATLAB代码注释详细。,基于FSSSA-DBN的深度分类预测算法的MATLAB代码注释
基于自适应粒子群算法的源储容量配置优化策略:考虑合作博弈与Shapley分配模型的研究报告,考虑合作博弈的源储容量配置代码 采用自适应粒子群算法编写 考虑shapley分配模型对收益进行分配 容量配置+优化调度 本人亲自编写,附参考文献,可改写性强,可。 ,合作博弈; 自适应粒子群算法; 容量配置优化调度; 收益分配模型(Shapley); 参考注释。,基于Shapley分配的容量配置优化与调度代码:自适应粒子群算法的实现
nodejs010-nodejs-editor-0.0.5-1.el6.centos.alt.noarch.rpm
免费JAVA毕业设计 2024成品源码+论文+录屏+启动教程 启动教程:https://www.bilibili.com/video/BV1SzbFe7EGZ 项目讲解视频:https://www.bilibili.com/video/BV1Tb421n72S 二次开发教程:https://www.bilibili.com/video/BV18i421i7Dx
《深入解析与复现:基于ICMIC混沌初始化的SHSSA算法及其与SSA的对比研究》,麻雀搜索算法(SSA)复现:《螺旋探索与自适应混合变异的麻雀搜索算法_陈功》 策略为:ICMIC混沌初始化种群+螺旋探索改进发现者策略+精英差分扰动策略+随机反向扰动策略——SHSSA 复现内容包括:改进SSA算法实现、23个基准测试函数、改进策略因子画图分析、相关混沌图分析、与SSA对比等。 程序基本上每一步都有注释,非常易懂,代码质量极高,便于新手学习和理解。 ,麻雀搜索算法(SSA)复现; 螺旋探索; 自适应混合变异; ICMIC混沌初始化种群; 策略因子画图分析; 代码质量高; 对比实验。,麻雀搜索算法(SSA)的SHSSA策略复现与对比分析
免费JAVA毕业设计 2024成品源码+论文+数据库+启动教程 启动教程:https://www.bilibili.com/video/BV1SzbFe7EGZ 项目讲解视频:https://www.bilibili.com/video/BV1Tb421n72S 二次开发教程:https://www.bilibili.com/video/BV18i421i7Dx
基于MATLAB/SIMULINK仿真的永磁同步电动机双闭环控制系统研究:变频侧五电平控制与整流侧三电平控制的实现与优化(默认MATLAB 2018b环境),MATLAB,SIMULINK仿真 永磁同步电动机,转速电流双闭环 变频侧五电平控制,整流侧三电平控制 默认MATLAB2018b ,MATLAB; SIMULINK仿真; 永磁同步电动机; 转速电流双闭环; 五电平控制; 三电平控制; MATLAB2018b,MATLAB中永磁同步电机双闭环五电平控制策略研究
《利用HFSS软件自制的角锥(矩形)喇叭天线模型:结果展示与参数化调整教程》,HFSS角锥(矩形)喇叭天线 天线模型,附带结果,可改参数,HFSS软件包 自己做的,保证正确(有教程,具体到每一步,可以自己做出来参考bao gao) ,HFSS; 角锥喇叭天线; 矩形; 天线模型; 附带结果; 可改参数; HFSS软件包; 自制; 保证正确; 教程,HFSS软件角锥喇叭天线模型:可改参数保证正确结果
1、文件内容:publican-redhat-2.7-6.el7.rpm以及相关依赖 2、文件形式:tar.gz压缩包 3、安装指令: #Step1、解压 tar -zxvf /mnt/data/output/publican-redhat-2.7-6.el7.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm 4、安装指导:私信博主,全程指导安装