A datasource defines a pool of JDBC connections for a specific database using a URL, username, and so on. JDBC datasources make it easy for an application to access data in a database server.
In a tc Runtime instance, you can create the following two types of JDBC datasources:
Database connection pool (DBCP) datasource
tc Runtime datasource
The DBCP datasource is the
standard datasource provided by tc Runtime; it uses the org.apache.commons.dbcp
package. Although this datasource is adequate for simple applications,
it is single-threaded, which means that in order to be thread-safe, the
tc Runtime instance must lock the entire pool, even during query
validation. Thus it is not suitable for highly concurrent environments.
Additionally, it can be slow, which in turn can negatively affect the
performance of Web applications.
The tc Runtime datasource includes all the functionality of the DBCP datasource, but adds additional features to support highly-concurrent environments and multiple core/cpu systems. The tc Runtime datasource typically performs much better than the DBCP datasource. Additional features include:
Dynamic implementation of the interfaces, which means that the
datasource supports the java.sql and
javax.sql interfaces for your runtime environment (as
long as your JDBC driver supports it), even when compiled with a
lower version of the JDK.
Validation intervals so that tc Runtime doesn't have to validate every single time the application uses the connection, which improves performance.
Run-Once query, which is a configurable query that tc Runtime runs only once when the connection to the database is established. This is very useful to set up session settings that you want to exist during the entire time the connection is established.
Ability to configure custom interceptors to enhance the
functionality of the datasource. You can use interceptors to gather
query stats, cache session states, reconnect the connection upon
failures, retry queries, cache query results, and so on. The
interceptors are dynamic and not tied to a JDK version of a
java.sql/javax.sql interface.
Asynchronous connection retrieval - you can queue your request for a connection and receive a Future<Connection> back.
As with any tc Runtime resource, you configure the
high-concurrency datasource (that is, the tc Runtime datasource) using a
<Resource> child element of
<GlobalNamingResource>. Most attributes are common to
the standard DBCP and the tc Runtime datasources; however, the following
new attributes apply only to the new tc Runtime datasource.
initSQL
jdbcInterceptors
validationInterval
jmxEnabled
fairQueue
useEquals
Use the factory attribute of the
<Resource> element to specify the type of
datasource:
Set the factory attribute to
org.apache.tomcat.jdbc.pool.DataSourceFactory to use
the tc Runtime high-concurrency datasource. This is also the default
value of the factory attribute for tc Runtime, so you
will automatically use the high-concurrency datasource if you do not
specify this attribute at all.
Set the factory attribute to
org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory to
use the standard DBCP datasource.
IBM JVM USERS ONLY: If you are using an IBM JVM, see useEquals for important information.
The following table lists the attributes for configuring either the high-concurrency datasource or the standard DBCP datasource. Most attributes are valid for both of the datasources, but some are only valid for one datasource. These exceptions are noted in the table. The default values shown are for the high-concurrency datasource, which is the default datasource for tc Server. Default values for the DBCP datasource may be different. See the Apache DBCP documentation for details.
Table 4.1. Connection Pool Configuration Attributes
The following server.xml snippet shows how to
configure the high-concurrency JDBC datasource for your tc Runtime
instance. You can add this datasource to a tc Server Runtime instance by
including the diagnostics template in the tcruntime-instance
create command line. For an explanation of the following example,
see Description of the High
Concurrency JDBC Datasource.
<?xml version='1.0' encoding='utf-8'?> <Server port="-1" shutdown="SHUTDOWN"> ... <GlobalNamingResources> <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" username="root" password="password" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/mysql?autoReconnect=true" testWhileIdle="true" testOnBorrow="true" testOnReturn="false" validationQuery="SELECT 1" validationInterval="30000" timeBetweenEvictionRunsMillis="5000" maxActive="100" minIdle="10" maxWait="10000" initialSize="10" removeAbandonedTimeout="60" removeAbandoned="true" logAbandoned="true" minEvictableIdleTimeMillis="30000" jmxEnabled="true" jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState; org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer; org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReportJmx(threshold=10000)"/> </GlobalNamingResources> ... <Service name="Catalina"> ... </Service> </Server>
In the preceding sample server.xml, the
<Resource> element does not include a
factory attribute, which means that the resource is using
the default value,
org.apache.tomcat.jdbc.pool.DataSourceFactory, the tc
Runtime high-concurrency datasource. The <Resource>
element attributes in the example function as follows:
name. JNDI name
of this JDBC resource is jdbc/TestDB.
auth. The
container signs on to the resource manager on behalf of the
application.
type. This
resource is a JDBC datasource.
username,
password. Name and password of the database
user who connects to the database.
driverClassName.
tc Runtime should use the com.mysql.jdbc.Driver JDBC
driver to connect to the database, in this case a MySQL
database.
url. URL that
the JDBC driver uses to connect to a MySQL database. The format of
this URL is specified by JDBC.
testXXX
attributes. tc Runtime validates objects before it
borrows them from the connection pool and those objects are
validated by the idle object evictor, but that tc Runtime does
not validate objects when it returns them to
the pool.
validationQuery.
tc Runtime runs the very simple SQL query SELECT 1 when
it validates connections from the pool before returning a connection
to a user upon request. Because this query should always return a
value, if it returns an exception then tc Runtime knows there is a
problem with the connection.
validationInterval. tc Runtime
waits at least 30 seconds before running a validation query.
timeBetweenEvictionRunsMillis.
tc Runtime sleeps 5000 milliseconds between runs of the idle
connection validation/cleaner thread.
maxActive. tc
Runtime allocates a maximum of 100 active connections from this pool
at the same time
minIdle. tc
Runtime keeps a minimum of 10 established connections in the pool at
all times.
maxWait. Where
no connections are available, tc Runtime waits a maximum of 10,000
milliseconds for a connection to be returned before throwing an
exception.
initialSize. tc
Runtime creates 10 connections when it initially starts the
connection pool.
removeAbandonedTimeout. tc
Runtime waits 60 seconds before it removes an abandoned, but still
in use, connection.
removeAbandoned.
tc Runtime removes abandoned connections after they have been idle
for the removeAbandonedTimeout amount of time.
logAbandoned. tc
Runtime flags to log stack traces for application code that
abandoned a Connection.
minEvictableIdleTimeMillis.
Minimum amount of time an object may sit idle in the pool before it
is eligible for eviction on this tc Runtime is 30,000
milliseconds.
jmxEnabled. This
tc Runtime can be monitored using JMX. You must set this attribute
to true if you want HQ to monitor the resource.
jdbcInterceptors. List of
interceptor classes associated with this datasource.
For complete documentation about the tc Runtime
server.xml file and all the possible XML elements you can
include, see Apache
Tomcat Configuration Reference.