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

(-)main/java/org/apache/tomcat/jdbc/pool/DataSourceFactory.java (-84 / +94 lines)
Lines 20-33 Link Here
20
import java.sql.Connection;
20
import java.sql.Connection;
21
import java.util.Hashtable;
21
import java.util.Hashtable;
22
import java.util.Properties;
22
import java.util.Properties;
23
24
import javax.management.ObjectName;
23
import javax.management.ObjectName;
25
import javax.naming.Context;
24
import javax.naming.*;
26
import javax.naming.InitialContext;
27
import javax.naming.Name;
28
import javax.naming.NamingException;
29
import javax.naming.RefAddr;
30
import javax.naming.Reference;
31
import javax.naming.spi.ObjectFactory;
25
import javax.naming.spi.ObjectFactory;
32
import javax.sql.DataSource;
26
import javax.sql.DataSource;
33
27
Lines 43-56 Link Here
43
 * <br/>
37
 * <br/>
44
 * Properties available for configuration:<br/>
38
 * Properties available for configuration:<br/>
45
 * <a href="http://commons.apache.org/dbcp/configuration.html">Commons DBCP properties</a><br/>
39
 * <a href="http://commons.apache.org/dbcp/configuration.html">Commons DBCP properties</a><br/>
46
 *<ol>
40
 * <ol>
47
 *  <li>initSQL - A query that gets executed once, right after the connection is established.</li>
41
 * <li>initSQL - A query that gets executed once, right after the connection is established.</li>
48
 *  <li>testOnConnect - run validationQuery after connection has been established.</li>
42
 * <li>testOnConnect - run validationQuery after connection has been established.</li>
49
 *  <li>validationInterval - avoid excess validation, only run validation at most at this frequency - time in milliseconds.</li>
43
 * <li>validationInterval - avoid excess validation, only run validation at most at this frequency - time in milliseconds.</li>
50
 *  <li>jdbcInterceptors - a semicolon separated list of classnames extending {@link JdbcInterceptor} class.</li>
44
 * <li>jdbcInterceptors - a semicolon separated list of classnames extending {@link JdbcInterceptor} class.</li>
51
 *  <li>jmxEnabled - true of false, whether to register the pool with JMX.</li>
45
 * <li>jmxEnabled - true of false, whether to register the pool with JMX.</li>
52
 *  <li>fairQueue - true of false, whether the pool should sacrifice a little bit of performance for true fairness.</li>
46
 * <li>fairQueue - true of false, whether the pool should sacrifice a little bit of performance for true fairness.</li>
53
 *</ol>
47
 * </ol>
48
 *
54
 * @author Craig R. McClanahan
49
 * @author Craig R. McClanahan
55
 * @author Dirk Verbeeck
50
 * @author Dirk Verbeeck
56
 * @author Filip Hanik
51
 * @author Filip Hanik
Lines 106-190 Link Here
106
    protected static final String PROP_USE_EQUALS = "useEquals";
101
    protected static final String PROP_USE_EQUALS = "useEquals";
107
    protected static final String PROP_USE_CON_LOCK = "useLock";
102
    protected static final String PROP_USE_CON_LOCK = "useLock";
108
103
109
    protected static final String PROP_DATASOURCE= "dataSource";
104
    protected static final String PROP_DATASOURCE = "dataSource";
110
    protected static final String PROP_DATASOURCE_JNDI = "dataSourceJNDI";
105
    protected static final String PROP_DATASOURCE_JNDI = "dataSourceJNDI";
111
106
112
    protected static final String PROP_SUSPECT_TIMEOUT = "suspectTimeout";
107
    protected static final String PROP_SUSPECT_TIMEOUT = "suspectTimeout";
113
108
114
    protected static final String PROP_ALTERNATE_USERNAME_ALLOWED = "alternateUsernameAllowed";
109
    protected static final String PROP_ALTERNATE_USERNAME_ALLOWED = "alternateUsernameAllowed";
115
110
111
    protected static final String PROP_COMMIT_ON_RETURN = "commitOnReturn";
116
112
113
117
    public static final int UNKNOWN_TRANSACTIONISOLATION = -1;
114
    public static final int UNKNOWN_TRANSACTIONISOLATION = -1;
118
115
119
    public static final String OBJECT_NAME = "object_name";
116
    public static final String OBJECT_NAME = "object_name";
120
117
121
118
122
    protected static final String[] ALL_PROPERTIES = {
119
    protected static final String[] ALL_PROPERTIES = {
123
        PROP_DEFAULTAUTOCOMMIT,
120
            PROP_DEFAULTAUTOCOMMIT,
124
        PROP_DEFAULTREADONLY,
121
            PROP_DEFAULTREADONLY,
125
        PROP_DEFAULTTRANSACTIONISOLATION,
122
            PROP_DEFAULTTRANSACTIONISOLATION,
126
        PROP_DEFAULTCATALOG,
123
            PROP_DEFAULTCATALOG,
127
        PROP_DRIVERCLASSNAME,
124
            PROP_DRIVERCLASSNAME,
128
        PROP_MAXACTIVE,
125
            PROP_MAXACTIVE,
129
        PROP_MAXIDLE,
126
            PROP_MAXIDLE,
130
        PROP_MINIDLE,
127
            PROP_MINIDLE,
131
        PROP_INITIALSIZE,
128
            PROP_INITIALSIZE,
132
        PROP_MAXWAIT,
129
            PROP_MAXWAIT,
133
        PROP_TESTONBORROW,
130
            PROP_TESTONBORROW,
134
        PROP_TESTONRETURN,
131
            PROP_TESTONRETURN,
135
        PROP_TIMEBETWEENEVICTIONRUNSMILLIS,
132
            PROP_TIMEBETWEENEVICTIONRUNSMILLIS,
136
        PROP_NUMTESTSPEREVICTIONRUN,
133
            PROP_NUMTESTSPEREVICTIONRUN,
137
        PROP_MINEVICTABLEIDLETIMEMILLIS,
134
            PROP_MINEVICTABLEIDLETIMEMILLIS,
138
        PROP_TESTWHILEIDLE,
135
            PROP_TESTWHILEIDLE,
139
        PROP_TESTONCONNECT,
136
            PROP_TESTONCONNECT,
140
        PROP_PASSWORD,
137
            PROP_PASSWORD,
141
        PROP_URL,
138
            PROP_URL,
142
        PROP_USERNAME,
139
            PROP_USERNAME,
143
        PROP_VALIDATIONQUERY,
140
            PROP_VALIDATIONQUERY,
144
        PROP_VALIDATOR_CLASS_NAME,
141
            PROP_VALIDATOR_CLASS_NAME,
145
        PROP_VALIDATIONINTERVAL,
142
            PROP_VALIDATIONINTERVAL,
146
        PROP_ACCESSTOUNDERLYINGCONNECTIONALLOWED,
143
            PROP_ACCESSTOUNDERLYINGCONNECTIONALLOWED,
147
        PROP_REMOVEABANDONED,
144
            PROP_REMOVEABANDONED,
148
        PROP_REMOVEABANDONEDTIMEOUT,
145
            PROP_REMOVEABANDONEDTIMEOUT,
149
        PROP_LOGABANDONED,
146
            PROP_LOGABANDONED,
150
        PROP_POOLPREPAREDSTATEMENTS,
147
            PROP_POOLPREPAREDSTATEMENTS,
151
        PROP_MAXOPENPREPAREDSTATEMENTS,
148
            PROP_MAXOPENPREPAREDSTATEMENTS,
152
        PROP_CONNECTIONPROPERTIES,
149
            PROP_CONNECTIONPROPERTIES,
153
        PROP_INITSQL,
150
            PROP_INITSQL,
154
        PROP_INTERCEPTORS,
151
            PROP_INTERCEPTORS,
155
        PROP_JMX_ENABLED,
152
            PROP_JMX_ENABLED,
156
        PROP_FAIR_QUEUE,
153
            PROP_FAIR_QUEUE,
157
        PROP_USE_EQUALS,
154
            PROP_USE_EQUALS,
158
        OBJECT_NAME,
155
            OBJECT_NAME,
159
        PROP_ABANDONWHENPERCENTAGEFULL,
156
            PROP_ABANDONWHENPERCENTAGEFULL,
160
        PROP_MAXAGE,
157
            PROP_MAXAGE,
161
        PROP_USE_CON_LOCK,
158
            PROP_USE_CON_LOCK,
162
        PROP_DATASOURCE,
159
            PROP_DATASOURCE,
163
        PROP_DATASOURCE_JNDI,
160
            PROP_DATASOURCE_JNDI,
164
        PROP_SUSPECT_TIMEOUT,
161
            PROP_SUSPECT_TIMEOUT,
165
        PROP_ALTERNATE_USERNAME_ALLOWED
162
            PROP_ALTERNATE_USERNAME_ALLOWED,
163
            PROP_COMMIT_ON_RETURN
164
166
    };
165
    };
167
166
167
168
    // -------------------------------------------------- ObjectFactory Methods
168
    // -------------------------------------------------- ObjectFactory Methods
169
169
170
    /**
170
    /**
171
     * <p>Create and return a new <code>BasicDataSource</code> instance.  If no
171
     * <p>Create and return a new <code>BasicDataSource</code> instance.  If no
172
     * instance can be created, return <code>null</code> instead.</p>
172
     * instance can be created, return <code>null</code> instead.</p>
173
     *
173
     *
174
     * @param obj The possibly null object containing location or
174
     * @param obj         The possibly null object containing location or
175
     *  reference information that can be used in creating an object
175
     *                    reference information that can be used in creating an object
176
     * @param name The name of this object relative to <code>nameCtx</code>
176
     * @param name        The name of this object relative to <code>nameCtx</code>
177
     * @param nameCtx The context relative to which the <code>name</code>
177
     * @param nameCtx     The context relative to which the <code>name</code>
178
     *  parameter is specified, or <code>null</code> if <code>name</code>
178
     *                    parameter is specified, or <code>null</code> if <code>name</code>
179
     *  is relative to the default initial context
179
     *                    is relative to the default initial context
180
     * @param environment The possibly null environment that is used in
180
     * @param environment The possibly null environment that is used in
181
     *  creating this object
181
     *                    creating this object
182
     *
182
     *
183
     * @exception Exception if an exception occurs creating the instance
183
     * @throws Exception if an exception occurs creating the instance
184
     */
184
     */
185
    @Override
185
    @Override
186
    public Object getObjectInstance(Object obj, Name name, Context nameCtx,
186
    public Object getObjectInstance(Object obj, Name name, Context nameCtx,
187
                                    Hashtable<?,?> environment) throws Exception {
187
                                    Hashtable<?, ?> environment) throws Exception {
188
188
189
        // We only know how to deal with <code>javax.naming.Reference</code>s
189
        // We only know how to deal with <code>javax.naming.Reference</code>s
190
        // that specify a class name of "javax.sql.DataSource"
190
        // that specify a class name of "javax.sql.DataSource"
Lines 206-212 Link Here
206
        }
206
        }
207
207
208
        if (!ok) {
208
        if (!ok) {
209
            log.warn(ref.getClassName()+" is not a valid class name/type for this JNDI factory.");
209
            log.warn(ref.getClassName() + " is not a valid class name/type for this JNDI factory.");
210
            return null;
210
            return null;
211
        }
211
        }
212
212
Lines 221-227 Link Here
221
            }
221
            }
222
        }
222
        }
223
223
224
        return createDataSource(properties,nameCtx,XA);
224
        return createDataSource(properties, nameCtx, XA);
225
    }
225
    }
226
226
227
    public static PoolConfiguration parsePoolProperties(Properties properties) {
227
    public static PoolConfiguration parsePoolProperties(Properties properties) {
Lines 402-412 Link Here
402
            poolProperties.setDbProperties(new Properties());
402
            poolProperties.setDbProperties(new Properties());
403
        }
403
        }
404
404
405
        if (poolProperties.getUsername()!=null) {
405
        if (poolProperties.getUsername() != null) {
406
            poolProperties.getDbProperties().setProperty("user",poolProperties.getUsername());
406
            poolProperties.getDbProperties().setProperty("user", poolProperties.getUsername());
407
        }
407
        }
408
        if (poolProperties.getPassword()!=null) {
408
        if (poolProperties.getPassword() != null) {
409
            poolProperties.getDbProperties().setProperty("password",poolProperties.getPassword());
409
            poolProperties.getDbProperties().setProperty("password", poolProperties.getPassword());
410
        }
410
        }
411
411
412
        value = properties.getProperty(PROP_INITSQL);
412
        value = properties.getProperty(PROP_INITSQL);
Lines 476-481 Link Here
476
            poolProperties.setAlternateUsernameAllowed(Boolean.parseBoolean(value));
476
            poolProperties.setAlternateUsernameAllowed(Boolean.parseBoolean(value));
477
        }
477
        }
478
478
479
        value = properties.getProperty(PROP_COMMIT_ON_RETURN);
480
        if (value != null) {
481
            poolProperties.setCommitOnReturn(Boolean.parseBoolean(value));
482
        }
483
479
        return poolProperties;
484
        return poolProperties;
480
    }
485
    }
481
486
Lines 484-500 Link Here
484
     * given properties.
489
     * given properties.
485
     *
490
     *
486
     * @param properties the datasource configuration properties
491
     * @param properties the datasource configuration properties
492
     *
487
     * @throws Exception if an error occurs creating the data source
493
     * @throws Exception if an error occurs creating the data source
488
     */
494
     */
489
    public DataSource createDataSource(Properties properties) throws Exception {
495
    public DataSource createDataSource(Properties properties) throws Exception {
490
        return createDataSource(properties,null,false);
496
        return createDataSource(properties, null, false);
491
    }
497
    }
492
    public DataSource createDataSource(Properties properties,Context context, boolean XA) throws Exception {
498
499
    public DataSource createDataSource(Properties properties, Context context, boolean XA) throws Exception {
493
        PoolConfiguration poolProperties = DataSourceFactory.parsePoolProperties(properties);
500
        PoolConfiguration poolProperties = DataSourceFactory.parsePoolProperties(properties);
494
        if (poolProperties.getDataSourceJNDI()!=null && poolProperties.getDataSource()==null) {
501
        if (poolProperties.getDataSourceJNDI() != null && poolProperties.getDataSource() == null) {
495
            performJNDILookup(context, poolProperties);
502
            performJNDILookup(context, poolProperties);
496
        }
503
        }
497
        org.apache.tomcat.jdbc.pool.DataSource dataSource = XA?
504
        org.apache.tomcat.jdbc.pool.DataSource dataSource = XA ?
498
                new org.apache.tomcat.jdbc.pool.XADataSource(poolProperties) :
505
                new org.apache.tomcat.jdbc.pool.XADataSource(poolProperties) :
499
                new org.apache.tomcat.jdbc.pool.DataSource(poolProperties);
506
                new org.apache.tomcat.jdbc.pool.DataSource(poolProperties);
500
        //initialise the pool itself
507
        //initialise the pool itself
Lines 506-540 Link Here
506
    public void performJNDILookup(Context context, PoolConfiguration poolProperties) {
513
    public void performJNDILookup(Context context, PoolConfiguration poolProperties) {
507
        Object jndiDS = null;
514
        Object jndiDS = null;
508
        try {
515
        try {
509
            if (context!=null) {
516
            if (context != null) {
510
                jndiDS = context.lookup(poolProperties.getDataSourceJNDI());
517
                jndiDS = context.lookup(poolProperties.getDataSourceJNDI());
511
            } else {
518
            } else {
512
                log.warn("dataSourceJNDI property is configued, but local JNDI context is null.");
519
                log.warn("dataSourceJNDI property is configued, but local JNDI context is null.");
513
            }
520
            }
514
        } catch (NamingException e) {
521
        } catch (NamingException e) {
515
            log.debug("The name \""+poolProperties.getDataSourceJNDI()+"\" can not be found in the local context.");
522
            log.debug("The name \"" + poolProperties.getDataSourceJNDI() + "\" can not be found in the local context.");
516
        }
523
        }
517
        if (jndiDS==null) {
524
        if (jndiDS == null) {
518
            try {
525
            try {
519
                context = new InitialContext();
526
                context = new InitialContext();
520
                jndiDS = context.lookup(poolProperties.getDataSourceJNDI());
527
                jndiDS = context.lookup(poolProperties.getDataSourceJNDI());
521
            } catch (NamingException e) {
528
            } catch (NamingException e) {
522
                log.warn("The name \""+poolProperties.getDataSourceJNDI()+"\" can not be found in the InitialContext.");
529
                log.warn("The name \"" + poolProperties.getDataSourceJNDI() + "\" can not be found in the InitialContext.");
523
            }
530
            }
524
        }
531
        }
525
        if (jndiDS!=null) {
532
        if (jndiDS != null) {
526
            poolProperties.setDataSource(jndiDS);
533
            poolProperties.setDataSource(jndiDS);
527
        }
534
        }
528
    }
535
    }
529
536
530
    /**
537
    /**
531
     * <p>Parse properties from the string. Format of the string must be [propertyName=property;]*<p>
538
     * <p>Parse properties from the string. Format of the string must be [propertyName=property;]*<p>
539
     *
532
     * @param propText
540
     * @param propText
541
     *
533
     * @return Properties
542
     * @return Properties
543
     *
534
     * @throws Exception
544
     * @throws Exception
535
     */
545
     */
536
    protected static Properties getProperties(String propText) {
546
    protected static Properties getProperties(String propText) {
537
        return PoolProperties.getProperties(propText,null);
547
        return PoolProperties.getProperties(propText, null);
538
    }
548
    }
539
549
540
}
550
}

Return to bug 53374