You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by Simon Sadedin <ss...@elogex.com> on 2002/05/07 19:57:01 UTC

Bug 8829 (NullPointerExcetion DBConnectionManager.getConnection)

All,

Having worked through and fixed this in my environment at least, i
thought i might as well post the patch.  To be honest, I don't fully
understand the code and if this will have some deleterious effect in a
multi-db environment or other situation.  But it works great for me.
There are two problems going on

1.  When the test is ended the database connections are closed while the
test is still running.  Here i added code to make the shutdown method
wait until all connections are returned before closing them (and
simultaneously not hand out new ones).  Hacky but mostly effective for
me.

2.  The JDBCSampler is caching the database connections, but they all
get closed at the end of the test (and wiped out).  There is no way to
make the JDBCSampler get new connections, but obviously the old ones are
dead.  My fix is just to stop the sampler caching them.  I don't know
why it was doing that in the first place since they are pooled anyway so
I don't really understand it, but hey it works for me.

Really I think all this needs to be addressed comprehensively, but
perhaps the following diffs will help somebody desperate.

Cheers,

Simon.

------------------------------------------------------------------------
-------------------
DBConnectionManager.java

75c75,76
<   Hashtable rentedConnections;
---
>   Hashtable rentedConnections;
>   boolean shuttingDown = false;
142c143,146
< 	 }catch(Exception e){}*/
---
> 	 }catch(Exception e){}*/
> 
>    System.out.println("Connection manager " + this + " is setting
up");
> 
175,179c179,201
< 	{
< 		Iterator iter = connections.keySet().iterator();
< 		while (iter.hasNext()) {
< 			close((DBKey)iter.next());
< 		}
---
> 	{
>     System.out.println("Connection manager " + this + " is shutting
down");
> 
>     try {
>       // Wait until all leases are returned - do not hand out any more
>       this.shuttingDown = true;
>       int waitCount = 0;
>       while(rentedConnections.size() > 0 && waitCount < 100) {
>         try { Thread.sleep(100); } catch (Exception ex) { };
>       }
> 
>       if(waitCount >= 100)
>         System.err.println("Warning: timeout while waiting for
databases to be returned!");
> 
>       Iterator iter = connections.keySet().iterator();
>       while (iter.hasNext()) {
>         close((DBKey)iter.next());
>       }
>       this.connections.clear();
>     }
>     finally {
>       this.shuttingDown = false;
>     }
189c211,216
<   {
---
>   {
>    if(shuttingDown == true) {
>      // caller may nullpointer - too bad, just don't give connections
while we're shutting down
>      return null;
>    }
> 

----------------------------------------------------------------

JDBCSampler.java

72,73c72,73
<  *@created    $Date: 2002/05/02 22:54:57 $
<  *@version    $Revision: 1.13 $
---
>  *@created    $Date: 2002/02/16 03:21:41 $
>  *@version    $Revision: 1.12 $
84c84
< 	private static Map keyMap = new HashMap();
---
> 	// private Map keyMap = new HashMap();
93,97d92
< 	
< 	public static void clearKeys()
< 	{
< 		keyMap.clear();
< 	}
117c112,113
< 		Statement stmt = null;
---
> 		// Statement stmt = null;
>     PreparedStatement stmt = null;
123,124c119
< 			int count = 0;
< 			while ((count < 20) && (con =
manager.getConnection(key)) == null)
---
> 			while ((con = manager.getConnection(key)) ==
null)
132d126
< 					count++;
135c129,132
< 			stmt = con.createStatement();
---
> 			// stmt = con.createStatement();
>       stmt = con.prepareStatement(sql.getQuery());
>       stmt.setFetchSize(50);
> 
137c134,137
< 			boolean retVal = stmt.execute(sql.getQuery());
---
> 			boolean retVal = stmt.execute();
> 
>       System.out.println(".");
> 
143c143
< 				rs.close();
---
> 				// rs.close();
149,150c149,150
< 			stmt.close();
< 			manager.releaseConnection(con);
---
> 			// stmt.close();
> 			// manager.releaseConnection(con);
154,176d153
< 			if (rs != null)
< 			{
< 				try
< 				{
< 					rs.close();
< 				}
< 				catch (SQLException err)
< 				{
< 					rs = null;
< 				}
< 			}
< 			if (stmt != null)
< 			{
< 				try
< 				{
< 					stmt.close();
< 				}
< 				catch (SQLException err)
< 				{
< 					stmt = null;
< 				}
< 			}
< 			manager.releaseConnection(con);
178c155,164
< 		}
---
> 		}
>     finally
>     {
>       if (rs != null) { try { rs.close();	} catch (SQLException
err) {} };
>       rs = null;
> 			if (stmt != null) { try { stmt.close(); } catch
(SQLException err) {} };
> 			stmt = null;
>       manager.releaseConnection(con);
>     }
> 
189,192c175,178
< 		DBKey key = (DBKey)keyMap.get(db.getUrl());
< 		if (key == null)
< 		{
< 			key = manager.getKey(db.getUrl(),
db.getUsername(), db.getPassword(),
---
> 		//DBKey key = (DBKey)keyMap.get(db.getUrl());
> 		//if (key == null)
> 		//{
> 			return manager.getKey(db.getUrl(),
db.getUsername(), db.getPassword(),
194,196c180,182
< 			keyMap.put(db.getUrl(), key);
< 		}
< 		return key;
---
> 		//	keyMap.put(db.getUrl(), key);
> 		//}
> 		//return key;

------------------------------------






--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>