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

(-)java/org/apache/tomcat/jdbc/pool/PooledConnection.java (-14 / +66 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.Properties;
23
import java.util.concurrent.locks.ReentrantReadWriteLock;
24
import java.util.concurrent.locks.ReentrantReadWriteLock;
24
25
25
import org.apache.juli.logging.Log;
26
import org.apache.juli.logging.Log;
Lines 40-45 Link Here
40
     */
41
     */
41
    private static final Log log = LogFactory.getLog(PooledConnection.class);
42
    private static final Log log = LogFactory.getLog(PooledConnection.class);
42
43
44
    public static final String PROP_USER = "user";
45
    
46
    public static final String PROP_PASSWORD = "password";
47
    
43
    /**
48
    /**
44
     * Validate when connection is borrowed flag
49
     * Validate when connection is borrowed flag
45
     */
50
     */
Lines 122-127 Link Here
122
        this.parent = parent;
127
        this.parent = parent;
123
    }
128
    }
124
129
130
    public boolean checkUser(String username, String password) {
131
        if (username==null) username = poolProperties.getUsername();
132
        if (password==null) password = poolProperties.getPassword();
133
        
134
        String storedUsr = (String)getAttributes().get(PROP_USER);
135
        String storedPwd = (String)getAttributes().get(PROP_PASSWORD);
136
        
137
        boolean result = (username==null && storedUsr==null);
138
        result = (result || (username!=null && username.equals(storedUsr)));
139
                
140
        result = result && ((password==null && storedPwd==null) || (password!=null && password.equals(storedPwd)));
141
        
142
        if (username==null)  getAttributes().remove(PROP_USER); else getAttributes().put(PROP_USER, username);
143
        if (password==null)  getAttributes().remove(PROP_PASSWORD); else getAttributes().put(PROP_PASSWORD, password);
144
        
145
        return result;
146
    }
147
    
125
    /**
148
    /**
126
     * Connects the underlying connection to the database.
149
     * Connects the underlying connection to the database.
127
     * @throws SQLException if the method {@link #release()} has been called.
150
     * @throws SQLException if the method {@link #release()} has been called.
Lines 162-171 Link Here
162
    }
185
    }
163
    
186
    
164
    protected void connectUsingDataSource() throws SQLException {
187
    protected void connectUsingDataSource() throws SQLException {
188
        String usr = null;
189
        String pwd = null;
190
        if (getAttributes().containsKey(PROP_USER)) {
191
            usr = (String) getAttributes().get(PROP_USER);
192
        } else {
193
            usr = poolProperties.getUsername();
194
            getAttributes().put(PROP_USER, usr);
195
        }
196
        if (getAttributes().containsKey(PROP_PASSWORD)) {
197
            pwd = (String) getAttributes().get(PROP_PASSWORD);
198
        } else {
199
            pwd = poolProperties.getPassword();
200
            getAttributes().put(PROP_PASSWORD, pwd);
201
        }
165
        if (poolProperties.getDataSource() instanceof javax.sql.XADataSource) {
202
        if (poolProperties.getDataSource() instanceof javax.sql.XADataSource) {
166
            javax.sql.XADataSource xds = (javax.sql.XADataSource)poolProperties.getDataSource();
203
            javax.sql.XADataSource xds = (javax.sql.XADataSource)poolProperties.getDataSource();
167
            if (poolProperties.getUsername()!=null && poolProperties.getPassword()!=null) {
204
            if (usr!=null && pwd!=null) {
168
                xaConnection = xds.getXAConnection(poolProperties.getUsername(), poolProperties.getPassword());
205
                xaConnection = xds.getXAConnection(usr, pwd);
169
                connection = xaConnection.getConnection();
206
                connection = xaConnection.getConnection();
170
            } else {
207
            } else {
171
                xaConnection = xds.getXAConnection();
208
                xaConnection = xds.getXAConnection();
Lines 173-187 Link Here
173
            }
210
            }
174
        } else if (poolProperties.getDataSource() instanceof javax.sql.DataSource){
211
        } else if (poolProperties.getDataSource() instanceof javax.sql.DataSource){
175
            javax.sql.DataSource ds = (javax.sql.DataSource)poolProperties.getDataSource();
212
            javax.sql.DataSource ds = (javax.sql.DataSource)poolProperties.getDataSource();
176
            if (poolProperties.getUsername()!=null && poolProperties.getPassword()!=null) {
213
            if (usr!=null && pwd!=null) {
177
                connection = ds.getConnection(poolProperties.getUsername(), poolProperties.getPassword());
214
                connection = ds.getConnection(usr, pwd);
178
            } else {
215
            } else {
179
                connection = ds.getConnection();
216
                connection = ds.getConnection();
180
            }
217
            }
181
        } else if (poolProperties.getDataSource() instanceof javax.sql.ConnectionPoolDataSource){
218
        } else if (poolProperties.getDataSource() instanceof javax.sql.ConnectionPoolDataSource){
182
            javax.sql.ConnectionPoolDataSource ds = (javax.sql.ConnectionPoolDataSource)poolProperties.getDataSource();
219
            javax.sql.ConnectionPoolDataSource ds = (javax.sql.ConnectionPoolDataSource)poolProperties.getDataSource();
183
            if (poolProperties.getUsername()!=null && poolProperties.getPassword()!=null) {
220
            if (usr!=null && pwd!=null) {
184
                connection = ds.getPooledConnection(poolProperties.getUsername(), poolProperties.getPassword()).getConnection();
221
                connection = ds.getPooledConnection(usr, pwd).getConnection();
185
            } else {
222
            } else {
186
                connection = ds.getPooledConnection().getConnection();
223
                connection = ds.getPooledConnection().getConnection();
187
            }
224
            }
Lines 203-219 Link Here
203
            throw ex;
240
            throw ex;
204
        }
241
        }
205
        String driverURL = poolProperties.getUrl();
242
        String driverURL = poolProperties.getUrl();
206
        String usr = poolProperties.getUsername();
243
        String usr = null;
207
        String pwd = poolProperties.getPassword();
244
        String pwd = null;
208
        if (usr != null) {
245
        if (getAttributes().containsKey(PROP_USER)) {
209
            poolProperties.getDbProperties().setProperty("user", usr);
246
            usr = (String) getAttributes().get(PROP_USER);
247
        } else {
248
            usr = poolProperties.getUsername();
249
            getAttributes().put(PROP_USER, usr);
210
        }
250
        }
211
        if (pwd != null) {
251
        if (getAttributes().containsKey(PROP_PASSWORD)) {
212
            poolProperties.getDbProperties().setProperty("password", pwd);
252
            pwd = (String) getAttributes().get(PROP_PASSWORD);
253
        } else {
254
            pwd = poolProperties.getPassword();
255
            getAttributes().put(PROP_PASSWORD, pwd);
213
        }
256
        }
214
        
257
        Properties properties = clone(poolProperties.getDbProperties());
258
        if (usr != null) properties.setProperty(PROP_USER, usr);
259
        if (pwd != null) properties.setProperty(PROP_PASSWORD, pwd);
260
215
        try {
261
        try {
216
            connection = driver.connect(driverURL, poolProperties.getDbProperties());
262
            connection = connection = driver.connect(driverURL, properties);
217
        } catch (Exception x) {
263
        } catch (Exception x) {
218
            if (log.isDebugEnabled()) {
264
            if (log.isDebugEnabled()) {
219
                log.debug("Unable to connect to database.", x);
265
                log.debug("Unable to connect to database.", x);
Lines 235-240 Link Here
235
        }
281
        }
236
    }
282
    }
237
    
283
    
284
    private Properties clone(Properties p) {
285
        Properties c = new Properties();
286
        c.putAll(p);
287
        return c;
288
    }
289
    
238
    /**
290
    /**
239
     * 
291
     * 
240
     * @return true if connect() was called successfully and disconnect has not yet been called
292
     * @return true if connect() was called successfully and disconnect has not yet been called
(-)java/org/apache/tomcat/jdbc/pool/ConnectionPool.java (-20 / +44 lines)
Lines 161-170 Link Here
161
     */
161
     */
162
    public Connection getConnection() throws SQLException {
162
    public Connection getConnection() throws SQLException {
163
        //check out a connection
163
        //check out a connection
164
        PooledConnection con = borrowConnection(-1);
164
        PooledConnection con = borrowConnection(-1,null,null);
165
        return setupConnection(con);
165
        return setupConnection(con);
166
    }
166
    }
167
167
168
       
169
    /**
170
     * Borrows a connection from the pool. If a connection is available (in the
171
     * idle queue) or the pool has not reached {@link PoolProperties#maxActive
172
     * maxActive} connections a connection is returned immediately. If no
173
     * connection is available, the pool will attempt to fetch a connection for
174
     * {@link PoolProperties#maxWait maxWait} milliseconds.
175
     * 
176
     * @return Connection - a java.sql.Connection/javax.sql.PooledConnection
177
     *         reflection proxy, wrapping the underlying object.
178
     * @throws SQLException
179
     *             - if the wait times out or a failure occurs creating a
180
     *             connection
181
     */
182
    public Connection getConnection(String username, String password) throws SQLException {
183
        // check out a connection
184
        PooledConnection con = borrowConnection(-1, username, password);
185
        return setupConnection(con);
186
    }
168
    
187
    
169
    /**
188
    /**
170
     * Returns the name of this pool
189
     * Returns the name of this pool
Lines 422-428 Link Here
422
        PooledConnection[] initialPool = new PooledConnection[poolProperties.getInitialSize()];
441
        PooledConnection[] initialPool = new PooledConnection[poolProperties.getInitialSize()];
423
        try {
442
        try {
424
            for (int i = 0; i < initialPool.length; i++) {
443
            for (int i = 0; i < initialPool.length; i++) {
425
                initialPool[i] = this.borrowConnection(0); //don't wait, should be no contention
444
                initialPool[i] = this.borrowConnection(0, null, null); //don't wait, should be no contention
426
            } //for
445
            } //for
427
446
428
        } catch (SQLException x) {
447
        } catch (SQLException x) {
Lines 529-535 Link Here
529
     * @return PooledConnection
548
     * @return PooledConnection
530
     * @throws SQLException
549
     * @throws SQLException
531
     */
550
     */
532
    private PooledConnection borrowConnection(int wait) throws SQLException {
551
    private PooledConnection borrowConnection(int wait, String username, String password) throws SQLException {
533
552
534
        if (isClosed()) {
553
        if (isClosed()) {
535
            throw new SQLException("Connection pool closed.");
554
            throw new SQLException("Connection pool closed.");
Lines 543-549 Link Here
543
        while (true) {
562
        while (true) {
544
            if (con!=null) {
563
            if (con!=null) {
545
                //configure the connection and return it
564
                //configure the connection and return it
546
                PooledConnection result = borrowConnection(now, con);
565
                PooledConnection result = borrowConnection(now, con, username, password);
547
                //null should never be returned, but was in a previous impl.
566
                //null should never be returned, but was in a previous impl.
548
                if (result!=null) return result;
567
                if (result!=null) return result;
549
            }
568
            }
Lines 559-565 Link Here
559
                    size.decrementAndGet();
578
                    size.decrementAndGet();
560
                } else {
579
                } else {
561
                    //create a connection, we're below the limit
580
                    //create a connection, we're below the limit
562
                    return createConnection(now, con);
581
                    return createConnection(now, con, username, password);
563
                }
582
                }
564
            } //end if
583
            } //end if
565
584
Lines 608-617 Link Here
608
     * @return a PooledConnection that has been connected
627
     * @return a PooledConnection that has been connected
609
     * @throws SQLException
628
     * @throws SQLException
610
     */
629
     */
611
    protected PooledConnection createConnection(long now,
630
    protected PooledConnection createConnection(long now, PooledConnection notUsed, String username, String password) throws SQLException {
612
            PooledConnection notUsed) throws SQLException {
613
        //no connections where available we'll create one
631
        //no connections where available we'll create one
614
        PooledConnection con = create();
632
        PooledConnection con = create();
633
        if (username!=null) con.getAttributes().put(con.PROP_USER, username);
634
        if (password!=null) con.getAttributes().put(con.PROP_PASSWORD, password);
615
        boolean error = false;
635
        boolean error = false;
616
        try {
636
        try {
617
            //connect and validate the connection
637
            //connect and validate the connection
Lines 660-672 Link Here
660
     * @return con
680
     * @return con
661
     * @throws SQLException if a validation error happens
681
     * @throws SQLException if a validation error happens
662
     */
682
     */
663
    protected PooledConnection borrowConnection(long now, PooledConnection con) throws SQLException {
683
    protected PooledConnection borrowConnection(long now, PooledConnection con, String username, String password) throws SQLException {
664
        //we have a connection, lets set it up
684
        //we have a connection, lets set it up
665
        
685
        
666
        //flag to see if we need to nullify
686
        //flag to see if we need to nullify
667
        boolean setToNull = false;
687
        boolean setToNull = false;
668
        try {
688
        try {
669
            con.lock();
689
            con.lock();
690
            boolean usercheck = con.checkUser(username, password);
670
            
691
            
671
            if (con.isReleased()) {
692
            if (con.isReleased()) {
672
                return null;
693
                return null;
Lines 676-695 Link Here
676
                //attempt to connect
697
                //attempt to connect
677
                con.connect();
698
                con.connect();
678
            }
699
            }
679
            if ((!con.isDiscarded()) && con.validate(PooledConnection.VALIDATE_BORROW)) {
700
            
680
                //set the timestamp
701
            if (usercheck) {
681
                con.setTimestamp(now);
702
                if ((!con.isDiscarded()) && con.validate(PooledConnection.VALIDATE_BORROW)) {
682
                if (getPoolProperties().isLogAbandoned()) {
703
                    //set the timestamp
683
                    //set the stack trace for this pool
704
                    con.setTimestamp(now);
684
                    con.setStackTrace(getThreadDump());
705
                    if (getPoolProperties().isLogAbandoned()) {
706
                        //set the stack trace for this pool
707
                        con.setStackTrace(getThreadDump());
708
                    }
709
                    if (!busy.offer(con)) {
710
                        log.debug("Connection doesn't fit into busy array, connection will not be traceable.");
711
                    }
712
                    return con;
685
                }
713
                }
686
                if (!busy.offer(con)) {
687
                    log.debug("Connection doesn't fit into busy array, connection will not be traceable.");
688
                }
689
                return con;
690
            }
714
            }
691
            //if we reached here, that means the connection
715
            //if we reached here, that means the connection
692
            //is either discarded or validation failed.
716
            //is either has another principal, is discarded or validation failed.
693
            //we will make one more attempt
717
            //we will make one more attempt
694
            //in order to guarantee that the thread that just acquired
718
            //in order to guarantee that the thread that just acquired
695
            //the connection shouldn't have to poll again.
719
            //the connection shouldn't have to poll again.
Lines 1051-1057 Link Here
1051
                if (result!=null) return result;
1075
                if (result!=null) return result;
1052
                if (configured.compareAndSet(false, true)) {
1076
                if (configured.compareAndSet(false, true)) {
1053
                    try {
1077
                    try {
1054
                        pc = borrowConnection(System.currentTimeMillis(),pc);
1078
                        pc = borrowConnection(System.currentTimeMillis(),pc, null, null);
1055
                        result = ConnectionPool.this.setupConnection(pc);
1079
                        result = ConnectionPool.this.setupConnection(pc);
1056
                    } catch (SQLException x) {
1080
                    } catch (SQLException x) {
1057
                        cause = x;
1081
                        cause = x;
(-)java/org/apache/tomcat/jdbc/pool/DataSourceProxy.java (-1 / +3 lines)
Lines 74-80 Link Here
74
     * {@link javax.sql.DataSource#getConnection()}
74
     * {@link javax.sql.DataSource#getConnection()}
75
     */
75
     */
76
    public Connection getConnection(String username, String password) throws SQLException {
76
    public Connection getConnection(String username, String password) throws SQLException {
77
        return getConnection();
77
        if (pool == null)
78
            return createPool().getConnection(username,password);
79
        return pool.getConnection(username,password);
78
    }
80
    }
79
81
80
    public PoolConfiguration getPoolProperties() {
82
    public PoolConfiguration getPoolProperties() {

Return to bug 50025