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

View differences:

PostgreSQLHelper.java
7 7
import java.util.ArrayList;
8 8
import java.util.List;
9 9
import org.apache.commons.dbcp.BasicDataSource;
10
import org.apache.commons.dbcp.BasicDataSourceFactory;
10 11
import org.apache.commons.lang3.BooleanUtils;
11 12
import org.apache.commons.lang3.StringUtils;
12 13
import org.gvsig.fmap.dal.DataTypes;
......
25 26
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory;
26 27
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCHelperBase;
27 28
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
28
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSRSsBase;
29
import org.gvsig.fmap.dal.store.jdbc2.spi.SRSSolverBase;
29 30
import org.gvsig.fmap.geom.Geometry;
30 31
import org.gvsig.fmap.geom.aggregate.MultiLine;
31 32
import org.gvsig.fmap.geom.aggregate.MultiPoint;
......
80 81
            if (this.dataSource == null) {
81 82
                this.dataSource = this.createDataSource();               
82 83
            }
83
            Connection conn = this.dataSource.getConnection();
84
            if( logger.isDebugEnabled() ) {
85
                logger.debug("getConnection:\n" + getStatusInformation());
86
            }
87
            Connection conn;
88
            try {
89
                conn = this.dataSource.getConnection();
90
            } catch(Throwable ex) {
91
                logger.debug("Error getting connection from pool.",ex);
92
                throw ex;
93
            }
94
            if( logger.isDebugEnabled() ) {
95
                logger.debug("Created connection: {}\n  NumActive: {}\n  NumIdle: {}",
96
                    new Object[] {
97
                        conn.hashCode(), 
98
                        this.dataSource.getNumActive(),
99
                        this.dataSource.getNumIdle()
100
                    }
101
                );
102
            }
84 103
            return conn;
85 104
        }
105
        
106
        public void closeConnection(Connection connection) {
107
            if( connection != null ) {
108
                int connectionId = connection.hashCode();
109
                try {
110
                    connection.close();
111
                } catch(Throwable ex) {
112
                    logger.warn("Can't close connection.", ex);
113
                }
114
                if( logger.isDebugEnabled() ) {
115
                    Boolean isClosed;
116
                    try {
117
                        isClosed = connection.isClosed();
118
                    } catch(Throwable th) {
119
                        isClosed = null;
120
                    }
121
                    logger.debug("Closed connection: {}\n  isClosed: {}\n  NumActive: {}\n  NumIdle: {}",
122
                        new Object[] {
123
                            connectionId, 
124
                            isClosed,
125
                            this.dataSource.getNumActive(),
126
                            this.dataSource.getNumIdle()
127
                        }
128
                    );
129
                }
130
           } else if( logger.isDebugEnabled() ) {
131
               logger.debug("Close connection: null");
132
           }
133
        }
134
        
135
        public String getStatusInformation() {
136
            StringBuilder builder = new StringBuilder();
137
            builder.append("BasicDataSource pool status:\n");
138
            builder.append("  Connection URL: '").append(this.dataSource.getUrl()).append("'\n");
139
            if( this.dataSource.getInitialSize()>0 ) {
140
                builder.append("  InitialSize: ").append(this.dataSource.getInitialSize()).append(" (The initial number of connections that are created when the pool is started)\n");
141
            }
142
            if( this.dataSource.isPoolPreparedStatements() ) {
143
                builder.append("  PoolPreparedStatements: ").append(this.dataSource.isPoolPreparedStatements()).append("\n");
144
                builder.append("  MaxOpenPreparedStatements: ").append(this.dataSource.getMaxOpenPreparedStatements()).append(" (The maximum number of open statements that can be allocated from the statement pool at the same time, or non-positive for no limit)\n");
145
            }
146
            builder.append("  MaxActive: ").append(this.dataSource.getMaxActive()).append(" (The maximum number of active connections that can be allocated from this pool at the same time)\n");
147
            builder.append("  MaxIdle: ").append(this.dataSource.getMaxIdle()).append(" (The maximum number of connections that can remain idle in the pool)\n");
148
            builder.append("  NumActive:").append(this.dataSource.getNumActive()).append(" (the current number of active connections)\n");
149
            builder.append("  NumIdle:").append(this.dataSource.getNumIdle()).append(" (the current number of idle connections)\n");
150
            return builder.toString();
151
        }
86 152

  
87 153
        private BasicDataSource createDataSource() throws SQLException {
88 154
            if (!this.isRegistered()) {
......
91 157
            PostgreSQLConnectionParameters params = connectionParameters;
92 158

  
93 159
            BasicDataSource ds = new BasicDataSource();
160
            ds.setMaxIdle(params.getMaxIdle());
94 161
            ds.setDriverClassName(params.getJDBCDriverClassName());
95 162
            if( params.getUseSSL() ) {
96 163
                String s = BooleanUtils.toStringTrueFalse(params.getUseSSL());
......
134 201
   
135 202
    public PostgreSQLHelper(JDBCConnectionParameters connectionParameters) {
136 203
        super(connectionParameters);
137
        this.srss = new JDBCSRSsBase(this);
204
        this.srssolver = new SRSSolverBase(this);
138 205
    }
139 206

  
140 207
    @Override
......
148 215
            throw new AccessResourceException(PostgreSQLLibrary.NAME, ex);
149 216
        }
150 217
    }
218

  
219
    @Override
220
    public void closeConnection(Connection connection) {
221
         this.connectionProvider.closeConnection(connection);
222
    }
151 223
    
224
    
225
    
152 226
    @Override
153 227
    public PostgreSQLConnectionParameters getConnectionParameters() {
154 228
        return (PostgreSQLConnectionParameters) super.getConnectionParameters();

Also available in: Unified diff