Reputation: 877
We plan to implement connection pooling as that appears to be the right mechanism to use when database connections are frequently opening and closing. We display our code below, but need to know where must we do the connection pooling definition. Should it be in the main()
method itself or in the ConnectionHandler
class? Another thing: what are the opinions regarding BoneCP?
public class cServer
{
class ConnectionHandler implements Runnable {
ConnectionHandler(Socket receivedSocketConn1) {
this.receivedSocketConn1=receivedSocketConn1;
}
public void run(){
createConnection();
processData();
closeConnection();
}
}
public void main()
{
try
{
final ServerSocket serverSocketConn = new ServerSocket(8000);
while (true){
try{
Socket socketConn1 = serverSocketConn.accept();
new Thread(new ConnectionHandler(socketConn1)).start();
}
catch(Exception e){
e.printStackTrace(System.out);
}
}
}
catch (Exception e){
e.printStackTrace(System.out);
}
}
}
Upvotes: 0
Views: 644
Reputation: 10833
Well the pool itself should not reside in the ConnectionHandler
, since it will be lost once the ConnectionHandler
finishes running. Nevertheless the ConnectionHandler
must have a reference to the pool, in order to be able to actually obtain a database connection. So I would say store the pool in your cServer
class, instantiate it in your main()
, and give each ConnectionHandler
a reference to it when the ConnectionHandler
is spawned.
I've used both C3P0 and DBCP but have never heard of Bone CP till now. It looks promising, but that's about all I can say about it. Here is a related post comparing C3P0 and DBCP.
Upvotes: 1
Reputation: 51915
If you're deploying in a Java EE container, use that container's connection pooling via JNDI.
If you're not deploying in a Java EE container, use a library such as BonePC, DBCP, or C3P0.
Upvotes: 1
Reputation: 13139
I would recommend to use existing connection pooling solutions (e.g. C3PO). Or use built-in db pools of an application server (all of them provide with such a feature).
Upvotes: 4