View | Details | Raw Unified | Return to bug 50025
Collapse All | Expand All

(-)java/org/apache/tomcat/jdbc/pool/PooledConnection.java (-11 / +48 lines)
Lines 20-25 Link Here
20
import java.sql.SQLException;
20
import java.sql.SQLException;
21
import java.sql.Statement;
21
import java.sql.Statement;
22
import java.util.HashMap;
22
import java.util.HashMap;
23
import java.util.Map;
24
import java.util.Properties;
23
import java.util.concurrent.locks.ReentrantReadWriteLock;
25
import java.util.concurrent.locks.ReentrantReadWriteLock;
24
26
25
import org.apache.juli.logging.Log;
27
import org.apache.juli.logging.Log;
Lines 36-41 Link Here
36
 * @version 1.0
38
 * @version 1.0
37
 */
39
 */
38
public class PooledConnection {
40
public class PooledConnection {
41
    public static final String PROP_USER = "user";
42
    public static final String PROP_PASSWORD = "user";
39
    /**
43
    /**
40
     * Logger
44
     * Logger
41
     */
45
     */
Lines 172-181 Link Here
172
    }
176
    }
173
    
177
    
174
    protected void connectUsingDataSource() throws SQLException {
178
    protected void connectUsingDataSource() throws SQLException {
179
        String usr = null;
180
        String pwd = null;
181
        if (getAttributes().containsKey(PROP_USER)) {
182
            usr = (String)getAttributes().get(PROP_USER);
183
        } else {
184
            usr = poolProperties.getUsername();
185
            getAttributes().put(PROP_USER, usr);
186
        }
187
        if (getAttributes().containsKey(PROP_PASSWORD)) {
188
            pwd = (String)getAttributes().get(PROP_PASSWORD);
189
        } else {
190
            pwd = poolProperties.getPassword();
191
            getAttributes().put(PROP_PASSWORD, pwd);
192
        }
175
        if (poolProperties.getDataSource() instanceof javax.sql.XADataSource) {
193
        if (poolProperties.getDataSource() instanceof javax.sql.XADataSource) {
176
            javax.sql.XADataSource xds = (javax.sql.XADataSource)poolProperties.getDataSource();
194
            javax.sql.XADataSource xds = (javax.sql.XADataSource)poolProperties.getDataSource();
177
            if (poolProperties.getUsername()!=null && poolProperties.getPassword()!=null) {
195
            if (usr!=null && pwd!=null) {
178
                xaConnection = xds.getXAConnection(poolProperties.getUsername(), poolProperties.getPassword());
196
                xaConnection = xds.getXAConnection(usr, pwd);
179
                connection = xaConnection.getConnection();
197
                connection = xaConnection.getConnection();
180
            } else {
198
            } else {
181
                xaConnection = xds.getXAConnection();
199
                xaConnection = xds.getXAConnection();
Lines 183-197 Link Here
183
            }
201
            }
184
        } else if (poolProperties.getDataSource() instanceof javax.sql.DataSource){
202
        } else if (poolProperties.getDataSource() instanceof javax.sql.DataSource){
185
            javax.sql.DataSource ds = (javax.sql.DataSource)poolProperties.getDataSource();
203
            javax.sql.DataSource ds = (javax.sql.DataSource)poolProperties.getDataSource();
186
            if (poolProperties.getUsername()!=null && poolProperties.getPassword()!=null) {
204
            if (usr!=null && pwd!=null) {
187
                connection = ds.getConnection(poolProperties.getUsername(), poolProperties.getPassword());
205
                connection = ds.getConnection(usr, pwd);
188
            } else {
206
            } else {
189
                connection = ds.getConnection();
207
                connection = ds.getConnection();
190
            }
208
            }
191
        } else if (poolProperties.getDataSource() instanceof javax.sql.ConnectionPoolDataSource){
209
        } else if (poolProperties.getDataSource() instanceof javax.sql.ConnectionPoolDataSource){
192
            javax.sql.ConnectionPoolDataSource ds = (javax.sql.ConnectionPoolDataSource)poolProperties.getDataSource();
210
            javax.sql.ConnectionPoolDataSource ds = (javax.sql.ConnectionPoolDataSource)poolProperties.getDataSource();
193
            if (poolProperties.getUsername()!=null && poolProperties.getPassword()!=null) {
211
            if (usr!=null && pwd!=null) {
194
                connection = ds.getPooledConnection(poolProperties.getUsername(), poolProperties.getPassword()).getConnection();
212
                connection = ds.getPooledConnection(usr, pwd).getConnection();
195
            } else {
213
            } else {
196
                connection = ds.getPooledConnection().getConnection();
214
                connection = ds.getPooledConnection().getConnection();
197
            }
215
            }
Lines 213-224 Link Here
213
            throw ex;
231
            throw ex;
214
        }
232
        }
215
        String driverURL = poolProperties.getUrl();
233
        String driverURL = poolProperties.getUrl();
216
        String usr = poolProperties.getUsername();
234
        String usr = null;
217
        String pwd = poolProperties.getPassword();
235
        String pwd = null;
218
        poolProperties.getDbProperties().setProperty("user", usr);
236
        if (getAttributes().containsKey(PROP_USER)) {
219
        poolProperties.getDbProperties().setProperty("password", pwd);
237
            usr = (String)getAttributes().get(PROP_USER);
238
        } else {
239
            usr = poolProperties.getUsername();
240
            getAttributes().put(PROP_USER, usr);
241
        }
242
        if (getAttributes().containsKey(PROP_PASSWORD)) {
243
            pwd = (String)getAttributes().get(PROP_PASSWORD);
244
        } else {
245
            pwd = poolProperties.getPassword();
246
            getAttributes().put(PROP_PASSWORD, pwd);
247
        }
248
        Properties properties = clone(poolProperties.getDbProperties());
249
        if (usr!=null) properties.setProperty(PROP_USER, usr);
250
        if (pwd!=null) properties.setProperty(PROP_PASSWORD, pwd);
220
        try {
251
        try {
221
            connection = driver.connect(driverURL, poolProperties.getDbProperties());
252
            connection = driver.connect(driverURL, properties);
222
        } catch (Exception x) {
253
        } catch (Exception x) {
223
            if (log.isDebugEnabled()) {
254
            if (log.isDebugEnabled()) {
224
                log.debug("Unable to connect to database.", x);
255
                log.debug("Unable to connect to database.", x);
Lines 240-245 Link Here
240
        }
271
        }
241
    }
272
    }
242
    
273
    
274
    private Properties clone(Properties p) {
275
        Properties c = new Properties();
276
        c.putAll(p);
277
        return c;
278
    }
279
    
243
    /**
280
    /**
244
     * 
281
     * 
245
     * @return true if connect() was called successfully and disconnect has not yet been called
282
     * @return true if connect() was called successfully and disconnect has not yet been called
(-)java/org/apache/tomcat/jdbc/pool/ConnectionPool.java (-6 / +22 lines)
Lines 153-158 Link Here
153
            throw new SQLException("Connection pool is misconfigured, doesn't support async retrieval. Set the 'fair' property to 'true'");
153
            throw new SQLException("Connection pool is misconfigured, doesn't support async retrieval. Set the 'fair' property to 'true'");
154
        }
154
        }
155
    }
155
    }
156
   
157
    /**
158
     * Borrows a connection from the pool. If a connection is available (in the idle queue) or the pool has not reached 
159
     * {@link PoolProperties#maxActive maxActive} connections a connection is returned immediately.
160
     * If no connection is available, the pool will attempt to fetch a connection for {@link PoolProperties#maxWait maxWait} milliseconds.
161
     * @return Connection - a java.sql.Connection/javax.sql.PooledConnection reflection proxy, wrapping the underlying object.
162
     * @throws SQLException - if the wait times out or a failure occurs creating a connection
163
     */
164
    public Connection getConnection(String username, String password) throws SQLException {
165
        //check out a connection
166
        PooledConnection con = borrowConnection(-1, username, password);
167
        return setupConnection(con);
168
    }
169
156
    
170
    
157
    /**
171
    /**
158
     * Borrows a connection from the pool. If a connection is available (in the idle queue) or the pool has not reached 
172
     * Borrows a connection from the pool. If a connection is available (in the idle queue) or the pool has not reached 
Lines 163-169 Link Here
163
     */
177
     */
164
    public Connection getConnection() throws SQLException {
178
    public Connection getConnection() throws SQLException {
165
        //check out a connection
179
        //check out a connection
166
        PooledConnection con = borrowConnection(-1);
180
        PooledConnection con = borrowConnection(-1,null,null);
167
        return setupConnection(con);
181
        return setupConnection(con);
168
    }
182
    }
169
183
Lines 424-430 Link Here
424
        PooledConnection[] initialPool = new PooledConnection[poolProperties.getInitialSize()];
438
        PooledConnection[] initialPool = new PooledConnection[poolProperties.getInitialSize()];
425
        try {
439
        try {
426
            for (int i = 0; i < initialPool.length; i++) {
440
            for (int i = 0; i < initialPool.length; i++) {
427
                initialPool[i] = this.borrowConnection(0); //don't wait, should be no contention
441
                initialPool[i] = this.borrowConnection(0, null, null); //don't wait, should be no contention
428
            } //for
442
            } //for
429
443
430
        } catch (SQLException x) {
444
        } catch (SQLException x) {
Lines 528-534 Link Here
528
     * @return PooledConnection
542
     * @return PooledConnection
529
     * @throws SQLException
543
     * @throws SQLException
530
     */
544
     */
531
    private PooledConnection borrowConnection(int wait) throws SQLException {
545
    private PooledConnection borrowConnection(int wait, String username, String password) throws SQLException {
532
546
533
        if (isClosed()) {
547
        if (isClosed()) {
534
            throw new SQLException("Connection pool closed.");
548
            throw new SQLException("Connection pool closed.");
Lines 542-548 Link Here
542
        while (true) {
556
        while (true) {
543
            if (con!=null) {
557
            if (con!=null) {
544
                //configure the connection and return it
558
                //configure the connection and return it
545
                PooledConnection result = borrowConnection(now, con);
559
                PooledConnection result = borrowConnection(now, con, username, password);
546
                //null should never be returned, but was in a previous impl.
560
                //null should never be returned, but was in a previous impl.
547
                if (result!=null) return result;
561
                if (result!=null) return result;
548
            }
562
            }
Lines 558-564 Link Here
558
                    size.decrementAndGet();
572
                    size.decrementAndGet();
559
                } else {
573
                } else {
560
                    //create a connection, we're below the limit
574
                    //create a connection, we're below the limit
561
                    return createConnection(now, con);
575
                    return createConnection(now, con, username, password);
562
                }
576
                }
563
            } //end if
577
            } //end if
564
578
Lines 607-618 Link Here
607
     * @return a PooledConnection that has been connected
621
     * @return a PooledConnection that has been connected
608
     * @throws SQLException
622
     * @throws SQLException
609
     */
623
     */
610
    protected PooledConnection createConnection(long now, PooledConnection con) throws SQLException {
624
    protected PooledConnection createConnection(long now, PooledConnection con, String username, String password) throws SQLException {
611
        //no connections where available we'll create one
625
        //no connections where available we'll create one
612
        boolean error = false;
626
        boolean error = false;
613
        try {
627
        try {
614
            //connect and validate the connection
628
            //connect and validate the connection
615
            con = create();
629
            con = create();
630
            if (username!=null) con.getAttributes().put(con.PROP_USER, username);
631
            if (password!=null) con.getAttributes().put(con.PROP_PASSWORD, password);
616
            con.lock();
632
            con.lock();
617
            con.connect();
633
            con.connect();
618
            if (con.validate(PooledConnection.VALIDATE_INIT)) {
634
            if (con.validate(PooledConnection.VALIDATE_INIT)) {
(-)java/org/apache/tomcat/jdbc/pool/DataSource.java (-1 lines)
Lines 24-30 Link Here
24
import javax.management.MBeanServer;
24
import javax.management.MBeanServer;
25
import javax.management.MalformedObjectNameException;
25
import javax.management.MalformedObjectNameException;
26
import javax.management.ObjectName;
26
import javax.management.ObjectName;
27
import javax.sql.XADataSource;
28
27
29
import org.apache.juli.logging.Log;
28
import org.apache.juli.logging.Log;
30
import org.apache.juli.logging.LogFactory;
29
import org.apache.juli.logging.LogFactory;

Return to bug 50025