File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Connection questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Connection questions" Watch "Connection questions" New topic
Author

Connection questions

Bob Young
Ranch Hand

Joined: Dec 23, 2000
Posts: 65
I am working through Jason Hunter, Java Servlet Programming and I have a question about a comment made in the text about an example. The chapter is about database connectivity and example 9-8 showcases a connection pool. Here is a cut and paste of part of the example:
synchronized (connections) {
while(cons.hasMoreElements()) {
con = (Connection)cons.nextElement();

Boolean b = (Boolean)connections.get(con);
if (b == Boolean.FALSE) {
// So we found an unused connection.
// Test its integrity with a quick setAutoCommit(true) call.
// For production use, more testing should be performed,
// such as executing a simple query.
try {
con.setAutoCommit(true);
}
catch(SQLException e) {
// Problem with the connection, replace it.
connections.remove(con);
con = getNewConnection();
}
// Update the Hashtable to show this one's taken
connections.put(con, Boolean.TRUE);
// Return the connection
return con;
}
}
In the text, the author states "For deployment, you probably want something that does a better job of maintaining the quality of the pool and does more verification of integtity than a simple call to setAutoCommit()." What quality is he refering to? Why wouldn't a call to setAutoCommit() be good enough? If you have a connection to the database, then you have a connection. What can go wrong on this? I have zero real experience with database connections. On my PC, it always connects, so I don't have anything to relate these statements to. Any insight would be appreciated.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Moving this from Servlets -> JDBC.

Maybe it will attract some attention there.
Kaustubh Patil
Ranch Hand

Joined: Aug 13, 2001
Posts: 164
Even if you have the connections pooled in with you lot of things can go wrong during the real execution.
The database server has the habbit of timming-out the connections that are idle for a long time. So its always better to check if the connection taken from the pool is open or closed.
the connection interface provides isClosed() method to check this.
Kaustubh.


Kaustubh. Mumbai, India.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connection questions
 
Similar Threads
DB connection in a servlet question
DataTap Connection pool usage?
Data source
Connection Pool
Pool connection leak error