Thread: [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 |
From: Bill H. <bi...@lo...> - 2004-03-11 10:20:03
|
Bertrand, On Thu, 2004-03-11 at 09:45, Bertrand Renuart wrote: > I noticed the 'new ProxyConnection()' could throw an SQLException. If > this happens, the *real* connection is not closed although it should > probably be... Well spotted. > 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... > ==> Wondering when this could happen? Agreed. It does seem a bit paranoid to test for null. The delegate driver should either return a connection of throw an exception. > - if it is because we failed to obtain a connection from the > DriverManager, then this should be tested in > ProxyFactory.buildConnection (code snippet above); Yep. I don't see why we don't do a test straight after asking for one. > - 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... That doesn't happen. The only reason why new ProxyConnection() will throw an SQLException is if tthe real connection is null, and I think we should test for that before getting to the stage of building the proxy connection. > (is it clear ?) Absolutely. > If the latter is true, then the ProxyFactory.buildConnection() should > be modified as follows: [snip] > try { > // Ooops - unable to build the proxy - close the > realConnection > if(realConnection!=null) realConnection.close(); > } > catch (SQLException e) { > // Forget it - not much todo about it :( > } [snip] Well, the latter isn't true. But I still think this try/catch is worth implementing. I don't think we need to catch SQLException (we may well remove it from the new ProxyConnection signature) but we should certainly catch RuntimeExceptions and make sure we close the real connection. - Bill |