Revision 831 trunk/org.gvsig.postgresql/org.gvsig.postgresql.provider/src/main/java/org/gvsig/postgresql/dal/PostgreSQLHelper.java

View differences:

PostgreSQLHelper.java
1 1

  
2 2
package org.gvsig.postgresql.dal;
3 3

  
4
import java.io.IOException;
4 5
import java.sql.Connection;
5 6
import java.sql.SQLException;
6
import java.util.logging.Level;
7 7
import org.apache.commons.dbcp2.BasicDataSource;
8 8
import org.apache.commons.lang3.BooleanUtils;
9 9
import org.apache.commons.lang3.StringUtils;
......
14 14
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
15 15
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
16 16
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCDriverClassNotFoundException;
17
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
17 18
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory;
19
import org.gvsig.fmap.dal.store.jdbc2.spi.ConnectionProvider;
18 20
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCHelperBase;
19 21
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
20 22
import org.gvsig.fmap.dal.store.jdbc2.spi.SRSSolverBase;
......
52 54
        return connectionURL;
53 55
    }
54 56

  
55
    private static class ConnectionProvider implements Disposable {
57
    private static class PostgreSQLConnectionProvider implements Disposable, ConnectionProvider {
56 58

  
57 59
        private static boolean needRegisterDriver = true;
58 60

  
......
60 62

  
61 63
        private PostgreSQLConnectionParameters connectionParameters;
62 64

  
63
        public ConnectionProvider(PostgreSQLConnectionParameters connectionParameters) {
65
        public PostgreSQLConnectionProvider(PostgreSQLConnectionParameters connectionParameters) {
64 66
            this.connectionParameters = connectionParameters;
65 67
        }
66 68

  
......
95 97
            return conn;
96 98
        }
97 99
        
98
        public void closeConnection(Connection connection) {
99
            if( connection != null ) {
100
                int connectionId = connection.hashCode();
101
                try {
102
                    connection.close();
103
                } catch(Throwable ex) {
104
                    LOGGER.warn("Can't close connection.", ex);
105
                }
106
                if( LOGGER.isDebugEnabled() ) {
107
                    Boolean isClosed;
108
                    try {
109
                        isClosed = connection.isClosed();
110
                    } catch(Throwable th) {
111
                        isClosed = null;
112
                    }
113
                    LOGGER.debug("Closed connection: {}\n  isClosed: {}\n  NumActive: {}\n  NumIdle: {}",
114
                        new Object[] {
115
                            connectionId, 
116
                            isClosed,
117
                            this.dataSource.getNumActive(),
118
                            this.dataSource.getNumIdle()
119
                        }
120
                    );
121
                }
122
           } else if( LOGGER.isDebugEnabled() ) {
123
               LOGGER.debug("Close connection: null");
124
           }
125
        }
126
        
127 100
        public String getStatusInformation() {
128 101
            StringBuilder builder = new StringBuilder();
129 102
            builder.append("BasicDataSource pool status:\n");
......
204 177
            this.connectionParameters = null;
205 178
        }
206 179

  
180
        @Override
181
        public String getStatus() {
182
            StringBuilder builder = new StringBuilder();
183
            builder.append("Pool: ");
184
            builder.append(JDBCUtils.getHexId(dataSource));
185
            builder.append(" Actives: ");
186
            builder.append(dataSource.getNumActive());
187
            builder.append("/");
188
            builder.append(dataSource.getMaxTotal()); //.getMaxActive());
189
            builder.append(" idle: ");
190
            builder.append(dataSource.getNumIdle());
191
            builder.append("/");
192
            builder.append(dataSource.getMinIdle());
193
            builder.append(":");
194
            builder.append(dataSource.getMaxIdle());
195
            return builder.toString();
196
        }
197

  
207 198
    }
208 199

  
209 200
    private ConnectionProvider connectionProvider = null;
210 201
   
211
    public PostgreSQLHelper() {
212
        super(null);
202
    /**
203
     * Constructor for use only for testing purposes.
204
     * 
205
     * @param connectionParameters
206
     * @param connectionProvider
207
     */
208
    public PostgreSQLHelper(PostgreSQLConnectionParameters connectionParameters, ConnectionProvider connectionProvider) { 
209
        super(connectionParameters);
213 210
        this.srssolver = new SRSSolverDumb(this);
211
        this.connectionProvider = connectionProvider;
214 212
    }
213
  
215 214

  
215

  
216 216
    public PostgreSQLHelper(JDBCConnectionParameters connectionParameters) {
217 217
        super(connectionParameters);
218 218
        this.srssolver = new SRSSolverBase(this);
......
234 234
              if( this.getConnectionParameters()==null ) {
235 235
                return null;
236 236
              }
237
              this.connectionProvider = new ConnectionProvider(this.getConnectionParameters());
237
              this.connectionProvider = new PostgreSQLConnectionProvider(this.getConnectionParameters());
238 238
            }
239 239
            return this.connectionProvider.getConnection();
240 240
        } catch (SQLException ex) {
......
244 244

  
245 245
    @Override
246 246
    public void closeConnection(Connection connection) {
247
      if( connection!=null ) { // In test ???
248
         this.connectionProvider.closeConnection(connection);
249
      }
247
        if (connection != null) { // In test ???
248
            LOGGER.debug("[" + JDBCUtils.getConnId(connection) + "] closeConnection " + this.connectionProvider.getStatus());
249
        }
250
        super.closeConnection(connection);
250 251
    }
251 252
    
252 253
    @Override

Also available in: Unified diff