[Proxool-developer] ProxyFactory.buildConnection() - code review
UNMAINTAINED!
Brought to you by:
billhorsman
From: Bertrand R. <ber...@mo...> - 2004-03-11 09:55:30
|
Code snippet from ProxyFactory:=20 =20 protected static ProxyConnection buildProxyConnection(...) throws SQLException { Connection realConnection =3D null; final String url =3D connectionPool.getDefinition().getUrl(); Properties info =3D connectionPool.getDefinition().getDelegateProperties(); realConnection =3D DriverManager.getConnection(url, info); Object delegate =3D Proxy.newProxyInstance( realConnection.getClass().getClassLoader(), realConnection.getClass().getInterfaces(), new ProxyConnection(realConnection, id, url, = connectionPool, status)); return (ProxyConnection) Proxy.getInvocationHandler(delegate); } I noticed the 'new ProxyConnection()' could throw an SQLException. If = this happens, the *real* connection is not closed although it should probably be... On the other hand, I had a look at why this constructor could throw the exception... And it can do so only if the 'realConnection' given as = argument is null - there is a test at the end of the constructor... =3D=3D> = Wondering when this could happen? - if it is because we failed to obtain a connection from the = DriverManager, then this should be tested in ProxyFactory.buildConnection (code snippet above); - if it is because part of the ProxyConnection constructor will set the field to null if a problem occurs (don't think so), then ok, an = SQLException could be thrown by the constructor - but then, the *real* connection = should be released by the ProxyFactory... (is it clear ?) If the latter is true, then the ProxyFactory.buildConnection() should be modified as follows: protected static ProxyConnection buildProxyConnection(long id, ConnectionPool connectionPool, int status) throws SQLException { final String url =3D connectionPool.getDefinition().getUrl(); // Build the *real* connection Properties info =3D connectionPool.getDefinition().getDelegateProperties(); Connection realConnection =3D DriverManager.getConnection(url, = info); =20 // Build a proxyConnection around it - release the *real* = connection if it fails ProxyConnection proxyConnection =3D null; try { proxyConnection =3D new ProxyConnection(realConnection, id, = url, connectionPool, status); } catch(SQLException se) { try { // Ooops - unable to build the proxy - close the realConnection if(realConnection!=3Dnull) realConnection.close(); } catch (SQLException e) { // Forget it - not much todo about it :( } =20 throw se; } =20 =20 // Initialise our delegate Object delegate =3D Proxy.newProxyInstance( realConnection.getClass().getClassLoader(), realConnection.getClass().getInterfaces(), proxyConnection); return (ProxyConnection) Proxy.getInvocationHandler(delegate); }=20 |