Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #37122

Re: Is this the way to work with try with resources

Path csiph.com!aioe.org!.POSTED!not-for-mail
From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: Is this the way to work with try with resources
Date Wed, 9 Aug 2017 23:03:46 -0400
Organization Aioe.org NNTP Server
Lines 212
Message-ID <omgieb$ocl$1@gioia.aioe.org> (permalink)
References <87poc6wb9n.fsf@munus.decebal.nl> <omdob4$cif$1@gioia.aioe.org> <omdovg$d4o$1@gioia.aioe.org> <87k22dux5g.fsf@munus.decebal.nl> <omgapf$dlf$1@gioia.aioe.org>
NNTP-Posting-Host PBuS014lhzVyHjLbXmo/ZQ.user.gioia.aioe.org
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 8bit
X-Complaints-To abuse@aioe.org
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1
X-Notice Filtered by postfilter v. 0.8.2
Content-Language en-US
Xref csiph.com comp.lang.java.programmer:37122

Show key headers only | View raw


On 8/9/2017 8:53 PM, Arne Vajhøj wrote:
> The "magic" sauce:

> public class Reveal {

Well magic or not - it did have some bad concurrency
problem as I did not get all the synchronized in.

New version below.

Arne

import java.io.PrintStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Set;

public class Reveal {
     public static interface TrackableResource {
         public String getId() throws SQLException;
     }
     private static Set<TrackableResource> reslst = new 
HashSet<TrackableResource>();
     public static class ResultSetWrapper implements InvocationHandler, 
TrackableResource {
         private static int count = 0;
         private int num;
         private ResultSet rs;
         private String parent;
         private String sql;
         public ResultSetWrapper(ResultSet rs, String parent) {
             this.rs = rs;
             this.parent = parent;
             this.sql = null;
             synchronized(reslst) {
                 count++;
                 num = count;
                 reslst.add(this);
             }
         }
         public ResultSetWrapper(ResultSet rs, String parent, String sql) {
             this.rs = rs;
             this.parent = parent;
             this.sql = sql;
             synchronized(reslst) {
                 count++;
                 num = count;
                 reslst.add(this);
             }
         }
         public Object invoke(Object proxy, Method method, Object[] 
args) throws Throwable {
             if(method.getName().equals("close")) {
                 synchronized(reslst) {
                     reslst.remove(this);
                 }
                 return method.invoke(rs, args);
             } else {
                 return method.invoke(rs, args);
             }
         }
         public String getId() throws SQLException {
             if(sql == null) {
                 return "ResultSet#" + num + " from " + parent;
             } else {
                 return "ResultSet#" + num + " (" + sql + ") from " + 
parent;
             }
         }
     }
     public static class PreparedStatementWrapper implements 
InvocationHandler, TrackableResource {
         private static int count = 0;
         private int num;
         private Statement stmt;
         private String parent;
         private String sql;
         public PreparedStatementWrapper(Statement stmt, String parent, 
String sql) {
             this.stmt = stmt;
             this.parent = parent;
             this.sql = sql;
             synchronized(reslst) {
                 count++;
                 num = count;
                 reslst.add(this);
             }
         }
         public Object invoke(Object proxy, Method method, Object[] 
args) throws Throwable {
             if(method.getName().equals("close")) {
                 synchronized(reslst) {
                     reslst.remove(this);
                 }
                 return method.invoke(stmt, args);
             } else if(method.getName().equals("executeQuery")) {
                 return 
(ResultSet)Proxy.newProxyInstance(getClass().getClassLoader(), new 
Class<?>[] { ResultSet.class },
                                                          new 
ResultSetWrapper((ResultSet)method.invoke(stmt, args), getId()));
             } else {
                 return method.invoke(stmt, args);
             }
         }
         public String getId() throws SQLException {
             return "PreparedStatement#" + num + " (" + sql + ") from " 
+ parent;
         }
     }
     public static class StatementWrapper implements InvocationHandler, 
TrackableResource {
         private static int count = 0;
         private int num;
         private Statement stmt;
         private String parent;
         public StatementWrapper(Statement stmt, String parent) {
             this.stmt = stmt;
             this.parent = parent;
             synchronized(reslst) {
                 count++;
                 num = count;
                 reslst.add(this);
             }
         }
         public Object invoke(Object proxy, Method method, Object[] 
args) throws Throwable {
             if(method.getName().equals("close")) {
                 synchronized(reslst) {
                     reslst.remove(this);
                 }
                 return method.invoke(stmt, args);
             } else if(method.getName().equals("executeQuery")) {
                 return 
(ResultSet)Proxy.newProxyInstance(getClass().getClassLoader(), new 
Class<?>[] { ResultSet.class },
                                                          new 
ResultSetWrapper((ResultSet)method.invoke(stmt, args), getId(), 
(String)args[0]));
             } else {
                 return method.invoke(stmt, args);
             }
         }
         public String getId() throws SQLException {
             return "Statement#" + num + " from " + parent;
         }
     }
     public static class ConnectionWrapper implements InvocationHandler, 
TrackableResource {
         private static int count = 0;
         private int num;
         private Connection con;
         public ConnectionWrapper(Connection con) {
             this.con = con;
             synchronized(reslst) {
                 count++;
                 num = count;
                 reslst.add(this);
             }
         }
         public Object invoke(Object proxy, Method method, Object[] 
args) throws Throwable {
             if(method.getName().equals("close")) {
                 synchronized(reslst) {
                     reslst.remove(this);
                 }
                 return method.invoke(con, args);
             } else if(method.getName().equals("createStatement")) {
                 return 
(Statement)Proxy.newProxyInstance(con.getClass().getClassLoader(), new 
Class<?>[] { Statement.class },
                                                          new 
StatementWrapper((Statement)method.invoke(con, args), getId()));
             } else if(method.getName().equals("prepareStatement")) {
                 return 
(PreparedStatement)Proxy.newProxyInstance(con.getClass().getClassLoader(), 
new Class<?>[] { PreparedStatement.class },
                                                                  new 
PreparedStatementWrapper((PreparedStatement)method.invoke(con, args), 
getId(), (String)args[0]));
             } else {
                 return method.invoke(con, args);
             }
         }
         public String getId() throws SQLException {
             return "Connection#" + num;
         }
     }
     public static Connection wrap(Connection con) {
         return 
(Connection)Proxy.newProxyInstance(con.getClass().getClassLoader(), new 
Class<?>[] { Connection.class },
                                                   new 
ConnectionWrapper(con));
     }
     public static void dumpOpen(PrintStream ps) throws SQLException {
         synchronized(reslst) {
             for(TrackableResource obj : reslst.toArray(new 
TrackableResource[0])) {
                 System.out.println("Unclosed: " + obj.getId());
             }
         }
     }
}

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-08 13:26 +0200
  Re: Is this the way to work with try with resources Martin Gregorie <martin@address-in-sig.invalid> - 2017-08-08 12:51 +0000
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:10 +0200
  Re: Is this the way to work with try with resources Patrick Roemer <sangamon@netcologne.de> - 2017-08-08 18:29 +0200
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:20 +0200
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:16 +0200
  Re: Is this the way to work with try with resources Patrick Roemer <sangamon@netcologne.de> - 2017-08-08 19:30 +0200
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 06:49 +0200
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:01 +0200
    Re: Is this the way to work with try with resources Patrick Roemer <sangamon@netcologne.de> - 2017-08-09 15:39 +0200
  Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-08 21:25 -0400
    Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-08 21:36 -0400
      Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:29 +0200
        Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-09 20:53 -0400
          Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-09 23:03 -0400
    Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-08 21:48 -0400
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 07:24 +0200
  Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 06:26 +0200
    Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 08:30 +0200
    Re: Is this the way to work with try with resources Andreas Leitgeb <avl@logic.at> - 2017-08-09 09:40 +0000
      Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 12:56 +0200
        Re: Is this the way to work with try with resources Andreas Leitgeb <avl@logic.at> - 2017-08-09 16:51 +0000
          Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 19:37 +0200
            Re: Is this the way to work with try with resources Andreas Leitgeb <avl@logic.at> - 2017-08-09 19:51 +0000
              Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 22:20 +0200
                Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 23:17 +0200
          Re: Is this the way to work with try with resources Patrick Roemer <sangamon@netcologne.de> - 2017-08-09 22:08 +0200
        Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-09 18:07 -0400
    Re: Is this the way to work with try with resources Arne Vajhøj <arne@vajhoej.dk> - 2017-08-09 18:05 -0400
  Re: Is this the way to work with try with resources Cecil Westerhof <Cecil@decebal.nl> - 2017-08-09 06:39 +0200

csiph-web