Java连接池实现 [英] Java Connection Pool implementation
本文介绍了Java连接池实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class ConnectionPool {
private static List< DBConnection> pool = null;
private static int available = 0;
private ConnectionPool(){}
public static DBConnection getConnection(){
if(pool == null){
pool = new ArrayList< DBConnection> ;
for(int i = 0; i <3; i ++){
try {
pool.add(new DBConnection());
可用++;
} catch(SQLException e){
e.printStackTrace();
}
}
}
if(pool.size()> 0){
available--;
return pool.remove(available);
} else {
return null;
}
}
public static void returnConnection(DBConnection c){
pool.add(c);
可用++;
}
}
我只使用一个数组,请求连接池连接使用它,然后将其返回到连接池。
连接连接= ConnectionPool.getConnection ;
connection.execute(insert into users values('+ user.getUsername()+','+ user.getPassword()+','+ user.getGroup() '+ banned +','+ broker + admin + sharehodler + company +'));
ConnectionPool.returnConnection(connection);
connection = null;
请与我联系。谢谢
解决方案
有一些要点会使此实现非常有问题。
- 线程安全。如果几个线程与池一起工作怎么办?
- 3个连接的静态最大池大小,也可以立即创建所有这些连接,无论是否需要。一个常见的策略是创建一堆连接,并在需要时创建一些连接,直到达到允许/配置的最大值。
- 您只有静态方法。应该可以有几个池,这意味着您需要
ConnectionPool
的实例。 - 没有办法传递host + dbname + user +
- 您不处理断开的连接 - 如果现有的连接断开,您可能必须重新创建连接。
- 使用配置值而不是静态值,请参阅第2点
- 最后:确实,有趣的是自己写这个东西 - 但如果你需要一个项目池,选择一个现有的,如 c3p0 或 tomcat连接池。
我确定还有更多要指出的,但除非这些都是固定的,否则不能继续使用。
can you look at my connection pool if it is a possible way to implement it?
public class ConnectionPool {
private static List<DBConnection> pool = null;
private static int available = 0;
private ConnectionPool() {}
public static DBConnection getConnection() {
if (pool == null) {
pool = new ArrayList<DBConnection>();
for (int i = 0; i < 3; i++) {
try {
pool.add(new DBConnection());
available++;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
if (pool.size() > 0) {
available--;
return pool.remove(available);
} else {
return null;
}
}
public static void returnConnection(DBConnection c) {
pool.add(c);
available++;
}
}
I'm using only one array and the client should ask the connection pool for a connection use it and then return it to the connection pool.
Connection connection = ConnectionPool.getConnection();
connection.execute("insert into users values('"+user.getUsername()+"', '"+user.getPassword()+"', '"+user.getGroup()+"', '"+banned+"', '"+broker+admin+sharehodler+company+"')");
ConnectionPool.returnConnection(connection);
connection = null;
Please I need feedback on this implementation. Thank you
解决方案
There are some points that make this implementation very problematic.
- Thread safety. What if several threads work with the pool? You are not locking the list on read/write access.
- Static maximum pool size of 3 connections, also you immediately create all of them, whether they are needed or not. A common strategy is to create a bunch of connections and create some more when needed, until the allowed/configured maximum is reached.
- You only have static methods. It should be possible to have several pools, meaning you need instances of
ConnectionPool
. - No way to pass host+dbname+user+password to the connections that are created.
- You don't deal with 'broken' connections - you may have to re-create a connection if an existing one screwed up. This is far more relevant than I thought before I started using pools.
- Use config values instead of static values, see point #2
- Lastly: sure, it's interesting to write this stuff yourself - but if you need a pool for a project, pick an existing one, such as c3p0 or the tomcat connection pool.
I'm sure there's more to point out, but unless these are fixed, there's no use in continuing.
这篇关于Java连接池实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文