"arparikh" wrote :
| Below is the code where we call getConnection to the connection object. We
are getting NullPointer where we do conn.close(). The thing that I am wondering
is if we are getting null conn, we should get null pointer at
conn.setAutoCommit in getConnection or conn.createStatement. But instead we are
getting on conn.close
|
| public void doSelect(String p_sql, int[] p_dataTypes) throws DBException
| {
| Connection conn = null;
| try
| {
| conn = DBConnMgr.getInstance().getConn(this.m_resourceId);
| }
| finally
| {
| try
| {
| }
| finally
| {
| try
| {
| conn.close();
| }
| catch (SQLException se)
| {
| m_logger.info("Failed to close DB connection");
| }
| }
| }
| }
|
| Thanks for the assistance
This is a basic java question. What do you think happens
when getConn() throws an exception? Do you think it still invokes
the finally block(s)?
Here are the correct patterns:
1) Allocate before entering try/finally
| Connection c = allocateConnection();
| try
| {
| }
| finally
| {
| // c cannot be null
| c.close();
| }
|
2) Allocate inside the try block (because you want only one catch block)
Therefore you must check in the finally whether you actually got a connection
| Connection c = null;
| try
| {
| c = allocateConnection();
| }
| catch (SQLException e)
| {
| }
| finally
| {
| // c could be null if allocateConnection() threw an exception
| if (c != null)
| c.close();
| }
|
View the original post :
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4021063#4021063
Reply to the post :
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4021063
_______________________________________________
jboss-user mailing list
[email protected]
https://lists.jboss.org/mailman/listinfo/jboss-user