Are there recommended programming practices for using JDBC connections?
A. The general rule is to get the connection from the connection pool as late as possible and give it back to the connection pool as soon as possible. Ensure that the connection is a method variable, and get and release the connection within the same method as where you use it (each thread should have its own connection). The cost of getting the connection is small, the prepared statement cache will reduce the preparation time, the set statements are small, the execute needs to be done no matter what the usage, and the close is small. It is not recommended to create the connection at ejbCreate/activate and close it on ejbRemove/pasivate.
The general rule is to get the connection from the connection pool as late as possible and give it back to the connection pool as soon as possible. Ensure that the connection is a method variable, and get and release the connection within the same method as where you use it (each thread should have its own connection). The cost of getting the connection is small, the prepared statement cache will reduce the preparation time, the set statements are small, the execute needs to be done no matter what the usage, and the close is small. It is not recommended to create the connection at ejbCreate/activate and close it on ejbRemove/pasivate. 23. Why should I not use DriverManager.getConnection? DriverManager.getConnection can cause a deadlock. In the server, all DriverManager calls are class-synchronized including many frequent calls that all drivers make, and JDBC drivers do a lot of synchronization internally.