Revision 45097

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.h2/src/main/java/org/gvsig/fmap/dal/store/h2/H2SpatialHelper.java
40 40
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
41 41
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCDriverClassNotFoundException;
42 42
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer;
43
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
43 44
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory;
44 45
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCHelperBase;
45 46
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
......
132 133
        public ConnectionProviderImpl(H2SpatialConnectionParameters connectionParameters) {
133 134
            this.connectionParameters = connectionParameters;
134 135
        }
136

  
137
        @Override
138
        public String getStatus() {
139
            StringBuilder builder = new StringBuilder();
140
            builder.append("Pool: ");
141
            builder.append(JDBCUtils.getHexId(dataSource));
142
            builder.append(" Actives: ");
143
            builder.append(dataSource.getNumActive());
144
            builder.append("/");
145
            builder.append(dataSource.getMaxActive());
146
            builder.append(" idle: ");
147
            builder.append(dataSource.getNumIdle());
148
            builder.append("/");
149
            builder.append(dataSource.getMinIdle());
150
            builder.append(":");
151
            builder.append(dataSource.getMaxIdle());
152
            return builder.toString();
153
        }
135 154
        
136 155
        @SuppressWarnings("ConvertToTryWithResources")
137 156
        public void shutdown() {
......
146 165
            try {
147 166
                if( dataSource!=null ) {
148 167
                    LOGGER.info("Clossing connection pool.");
149
                    LOGGER.info("  Actives: "+dataSource.getNumActive());
150
                    LOGGER.info("  Idle   : "+dataSource.getNumIdle());
168
                    LOGGER.info(this.getStatus());
151 169
                    dataSource.close();
152 170
                    LOGGER.info("Connection pool closed.");
153
                    LOGGER.info("  Actives: "+dataSource.getNumActive());
154
                    LOGGER.info("  Idle   : "+dataSource.getNumIdle());
171
                    LOGGER.info(this.getStatus());
155 172
                }
156 173
            } catch (Throwable th) {
157 174
                LOGGER.warn("Problems closing connections pool.", th);
......
230 247
        
231 248
        @Override
232 249
        public Connection getConnection() throws SQLException {
233
//            File f = getLocalFile(connectionParameters);
234
//            boolean newdb = f != null && !f.exists();
235

  
236 250
            if (this.dataSource == null) {
237 251
                this.dataSource = this.createDataSource();               
238 252
            }
......
240 254
            try {
241 255
                conn = this.dataSource.getConnection();
242 256
            } catch(Throwable th) {
257
                LOGGER.warn("Can't create connection to '"+this.dataSource.getUrl()+"'. "+this.getStatus());
243 258
                LOGGER.warn("Can't create connection to '"+this.dataSource.getUrl()+"'.",th);
244 259
                throw th;
245 260
            }
......
357 372
            LOGGER.warn("Problems shutdown H2", ex);
358 373
        }
359 374
    }
375

  
376
    private void logConnectionStatus(String msg, Connection conn) {
377
        ConnectionProvider cp = this.getConnectionProvider();
378
        StringBuilder builder = new StringBuilder();
379
        builder.append(msg);
380
        if( conn == null ) {
381
            builder.append(": connection null");
382
        } else {
383
            Boolean closed = null;
384
            try {
385
                closed = conn.isClosed();
386
            } catch(Throwable th) {
387
            }
388
            builder.append(": connection ");
389
            builder.append(JDBCUtils.getConnId(conn));
390
            if( closed ) {
391
                builder.append(" (c)");
392
            }
393
            builder.append(" ");
394
        }
395
        builder.append(cp.getStatus());
396
        LOGGER.info(builder.toString());
397
    }
398
        
399
    private ConnectionProvider getConnectionProvider() {
400
        if (this.connectionProvider == null) {
401
          H2SpatialConnectionParameters connectionParameters = this.getConnectionParameters();
402
          if( connectionParameters==null ) {
403
            return null; // Testing mode?
404
          }
405
          this.connectionProvider = new ConnectionProviderImpl(connectionParameters);
406
        }
407
        return this.connectionProvider;
408
    }
360 409
    
361 410
    @Override
362 411
    public synchronized Connection  getConnection() throws AccessResourceException {
......
370 419
            }
371 420
            Connection connection = this.connectionProvider.getConnection();
372 421
            if( LOGGER.isDebugEnabled() ) {
373
                LOGGER.debug("getConnection: connection = "+connection.hashCode()+ connectionProvider.toString());
422
                LOGGER.debug("["+JDBCUtils.getConnId(connection)+"] getConnection "+connectionProvider.getStatus()+" "+ connectionProvider.toString());
374 423
            }
375 424
            return connection;
376 425
        } catch (SQLException ex) {
......
381 430
    @Override
382 431
    public void closeConnection(Connection connection) {
383 432
      if( connection!=null ) { // In test ???
384
        LOGGER.debug("closeConnection: connection = "+connection.hashCode());
433
        LOGGER.debug("["+JDBCUtils.getConnId(connection)+"] closeConnection "+ this.connectionProvider.getStatus());
385 434
      }
386 435
      super.closeConnection(connection);
387 436
    }
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/java/org/gvsig/fmap/dal/store/jdbc2/spi/JDBCHelperBase.java
372 372

  
373 373
  @Override
374 374
  public void closeConnection(Connection connection) {
375
    if (connection != null) {
376
      LOGGER.trace("Clossing connection " + connection.hashCode());
377
      try {
378
        connection.close();
379
      } catch (Exception ex) {
380
        LOGGER.warn("Can't close connection.", ex);
381
      }
382
    }
375
      JDBCUtils.close(connection);
383 376
  }
384 377

  
385 378
  @Override
386 379
  public void closeConnectionQuietly(Connection connection) {
387
    if (connection != null) {
388
      LOGGER.trace("Clossing connection quietly " + connection.hashCode());
389
      try {
390
        connection.close();
391
      } catch (Exception ex) {
392
        LOGGER.warn("Can't close connection.", ex);
393
      }
394
    }
380
      JDBCUtils.closeQuietly(connection);
395 381
  }
396 382

  
397 383
  @Override
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/java/org/gvsig/fmap/dal/store/jdbc2/spi/FakeConnectionProvider.java
42 42
    public FakeConnectionProvider(JDBCConnectionParameters parameters) {
43 43
        this();
44 44
    }
45

  
46
    @Override
47
    public String getStatus() {
48
        return "Fake connection provider";
49
    }
45 50
    
46 51
    @Override
47 52
    public Connection getConnection() throws SQLException {
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/java/org/gvsig/fmap/dal/store/jdbc2/spi/JDBCSQLBuilderBase.java
39 39
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
40 40
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
41 41
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
42
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
42 43
import org.gvsig.fmap.geom.Geometry;
43 44
import org.gvsig.fmap.geom.GeometryLocator;
44 45
import org.gvsig.fmap.geom.GeometryManager;
......
266 267
        }
267 268
        if( LOGGER.isDebugEnabled() ) {
268 269
            StringBuilder debug = new StringBuilder();
269
            debug.append("st.set(");
270
            debug.append("[");
271
            debug.append(JDBCUtils.getConnId(st));
272
            debug.append("] st.set(");
270 273
            try {
271 274
                byte[] bytes;
272 275
                int columnIndex = 1;
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/java/org/gvsig/fmap/dal/store/jdbc2/spi/ConnectionProvider.java
36 36

  
37 37
    public void registerDriver() throws SQLException;
38 38
    
39
    public String getStatus();
40
    
39 41
}
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/java/org/gvsig/fmap/dal/store/jdbc2/impl/ResulSetControlerBase.java
42 42
            this.extraValueNames = extraValueNames;
43 43
            used();
44 44
            resulSets.put(this.getID(), this);
45
            if( LOGGER.isDebugEnabled() ) {
46
                LOGGER.debug("["+JDBCUtils.getConnId(resulSet)+"] ResultSetEntryBase "+JDBCUtils.getHexId(this)+" "+sql);
47
            }
45 48
        }
46 49

  
47 50
        private void used() {
......
101 104

  
102 105
        @Override
103 106
        public void close() throws Exception {
107
            if( LOGGER.isDebugEnabled() ) {
108
                LOGGER.debug("["+JDBCUtils.getConnId(this.resultSet)+"] ResultSetEntryBase close "+JDBCUtils.getHexId(this));
109
            }
104 110
            if( this.resultSet == null ) {
105 111
                // Already close
106 112
                return;
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/java/org/gvsig/fmap/dal/store/jdbc2/JDBCUtils.java
50 50
            return sql;
51 51
        }
52 52
    }
53

  
54
    public static String getConnId(Statement st) {
55
        if( st==null ) {
56
            return "null";
57
        }
58
        try {
59
            return getHexId(st.getConnection());
60
        } catch (SQLException ex) {
61
            return "null";
62
        }
63
    }
64

  
65
    public static String getConnId(ResultSet resulSet) {
66
        if( resulSet==null ) {
67
            return "null";
68
        }
69
        try {
70
            return getConnId(resulSet.getStatement());
71
        } catch (SQLException ex) {
72
            return "null";
73
        }
74
    }
75

  
76
    public static String getConnId(Connection conn) {
77
        return getHexId(conn);
78
    }
53 79
    
80
    public static String getHexId(Object obj) {
81
        if( obj==null ) {
82
            return "null";
83
        }
84
        return Integer.toHexString(obj.hashCode()).toUpperCase();
85
    }
86
    
54 87
    public static ResultSet executeQuery(Statement st, String sql) throws SQLException {
55
        LOGGER.debug("execute query SQL: " + sql);
88
        if( LOGGER.isDebugEnabled() ) {
89
            LOGGER.debug("["+getConnId(st)+"] executeQuery(st) SQL= " + sql);
90
        }
56 91
        try {
92
            @SuppressWarnings("null")
57 93
            ResultSet rs = st.executeQuery(sql);
58 94
            return rs;
59 95
        } catch(Exception ex) {
......
62 98
        }
63 99
    }
64 100

  
101
    @SuppressWarnings("null")
65 102
    public static void execute(Statement st, String sql) throws SQLException {
66
        LOGGER.debug("execute SQL: " + sql);
103
        if( LOGGER.isDebugEnabled() ) {
104
            LOGGER.debug("["+getConnId(st)+"] execute(st) SQL: " + sql);
105
        }
67 106
        try {
68 107
            st.execute(sql);
69 108
        } catch(Exception ex) {
......
73 112
    }
74 113

  
75 114
    public static void execute(Connection connection, String sql) throws SQLException {
76
        LOGGER.debug("execute SQL: " + sql);
77
        Statement st = connection.createStatement();
115
        if( LOGGER.isDebugEnabled() ) {
116
            LOGGER.debug("["+getConnId(connection)+"] execute(conn) SQL: " + sql);
117
        }
78 118
        try {
119
            @SuppressWarnings("null")
120
            Statement st = connection.createStatement();
79 121
            st.execute(sql);
80 122
        } catch(Exception ex) {
81 123
            LOGGER.warn("execute SQL: " + sql, ex);
......
91 133
            Iterator<String> it = sqls.iterator();
92 134
            while( it.hasNext() ) {
93 135
                sql = it.next();
94
                LOGGER.debug("execute SQL: " + sql);
136
                if( LOGGER.isDebugEnabled() ) {
137
                    LOGGER.debug("["+getConnId(connection)+"] execute(conn) SQLs: " + sql);
138
                }
95 139
                st.execute(sql);
96 140
            }
97 141
        } catch (SQLException ex) {
......
104 148

  
105 149
    public static ResultSet executeQuery(PreparedStatement st, String sql) throws SQLException {
106 150
        if( LOGGER.isDebugEnabled() ) {
107
            LOGGER.debug("execute query SQL= "+ getSQLInfo(st,sql));
151
            LOGGER.debug("["+getConnId(st)+"] executeQuery(pst) SQL= "+ getSQLInfo(st,sql));
108 152
        }
109 153
        try {
154
            @SuppressWarnings("null")
110 155
            ResultSet rs = st.executeQuery();
111 156
            return rs;
112 157
        } catch(Exception ex) {
......
115 160
        }
116 161
    }
117 162

  
163
    @SuppressWarnings("null")
118 164
    public static void execute(PreparedStatement st, String sql) throws SQLException {
119 165
        if( LOGGER.isDebugEnabled() ) {
120
            LOGGER.debug("execute SQL= "+ getSQLInfo(st,sql));
166
            LOGGER.debug("["+getConnId(st)+"] execute(pst) SQL= "+ getSQLInfo(st,sql));
121 167
        }
122 168
        try {
123 169
            st.execute();
......
129 175

  
130 176
    public static int executeUpdate(PreparedStatement st, String sql) throws SQLException {
131 177
        if( LOGGER.isDebugEnabled() ) {
132
            LOGGER.debug("execute update SQL= "+ getSQLInfo(st,sql));
178
            LOGGER.debug("["+getConnId(st)+"] executeUpdate(pst) SQL= "+ getSQLInfo(st,sql));
133 179
        }
134 180
        try {
135 181
            return st.executeUpdate();
......
139 185
        }
140 186
    }
141 187

  
142
    public static void closeQuietly(Statement obj) {
143
        if (obj == null) {
188
    public static void closeQuietly(Statement st) {
189
        if( LOGGER.isDebugEnabled() ) {
190
            LOGGER.debug("["+getConnId(st)+"] Close statement");
191
        }
192
        if (st == null) {
144 193
            return;
145 194
        }
146 195
        try {
147
            obj.close();
196
            st.close();
148 197
        } catch (Exception e) {
149
            LOGGER.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e);
198
            LOGGER.warn("Problems closing " + st.getClass().getSimpleName() + " '" + st.toString() + "'.", e);
150 199
        }
151 200
    }
152 201

  
153
    public static void closeQuietly(ResultSet obj) {
154
        if (obj == null) {
202
    public static void closeQuietly(ResultSet resulSet) {
203
        if( LOGGER.isDebugEnabled() ) {
204
            LOGGER.debug("["+getConnId(resulSet)+"] Close ResultSet");
205
        }
206
        if (resulSet == null) {
155 207
            return;
156 208
        }
157 209
        try {
158
            obj.close();
210
            resulSet.close();
159 211
        } catch (Exception e) {
160
            LOGGER.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e);
212
            LOGGER.warn("Problems closing " + resulSet.getClass().getSimpleName() + " '" + resulSet.toString() + "'.", e);
161 213
        }
162 214
    }
163 215

  
164 216
    public static void closeQuietly(Connection conn) {
217
        if( LOGGER.isDebugEnabled() ) {
218
            LOGGER.debug("["+getConnId(conn)+"] Close conn ("+Objects.toString(conn)+")");
219
        }
165 220
        if (conn == null) {
166 221
            return;
167 222
        }
168
        LOGGER.trace("Closing connection "+ conn.hashCode() + " ("+conn.toString()+")");
169 223
        try {
170 224
            conn.close();
171 225
        } catch (Exception e) {
......
173 227
        }
174 228
    }
175 229
    
230
    public static void close(Connection conn) {
231
        if( LOGGER.isDebugEnabled() ) {
232
            LOGGER.debug("["+getConnId(conn)+"] Close conn ("+Objects.toString(conn)+")");
233
        }
234
        if (conn == null) {
235
            return;
236
        }
237
        try {
238
            conn.close();
239
        } catch (Exception e) {
240
            throw new RuntimeException(e);
241
        }
242
    }
243
    
176 244
    public static void closeQuietly(AutoCloseable obj) {
177 245
        if (obj == null) {
178 246
            return;
179 247
        }
180 248
        try {
181
            LOGGER.trace("Closing " + obj.getClass().getSimpleName() + " "+ obj.hashCode() + " ("+obj.toString()+")");
249
            if( LOGGER.isDebugEnabled() ) {
250
                LOGGER.debug("Closing " + obj.getClass().getSimpleName() + " "+ obj.hashCode() + " ("+obj.toString()+")");
251
            }
182 252
            obj.close();
183 253
        } catch (Exception e) {
184 254
            LOGGER.warn("Problems closing " + obj.getClass().getSimpleName() + " '" + obj.toString() + "'.", e);

Also available in: Unified diff