How do I pool Oracle connections in Tomcat? - oracle

I'm unclear on the appropriate way to set up connections to an Oracle RAC database in Tomcat via context.xml. THIS method works for me:
<Resource
name="jdbc/mydb"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:<connection details>"
username="<username>"
password="<account>"
maxTotal="150"
maxIdle="10"
/>
But is that using a connection pool? I tried adding factory="org.apache.tomcat.jdbc.pool.DataSourceFactory", as suggested at https://tomcat.apache.org/tomcat-8.0-doc/jdbc-pool.html, and mysteriously with that one change, I started getting:
java.sql.SQLException: ORA-01017: invalid username/password; logon denied
If I instead try type="oracle.jdbc.pool.OracleDataSource" and factory="oracle.jdbc.pool.OracleDataSourceFactory" I get:
Error: Unable to obtain a JDBC connection from Datasource:
java.lang.Exception: Error: Unable to obtain a JDBC connection from Datasource:
I've looked all over, but it's not clear to me what the best practice is for this. Ideas?

I am adding this answer to clarify my earlier comments, and for future visitors to this question.
Summary
Both of the following approaches will use DB connection pools:
Use Tomcat's newer, default DBCP 2 pool with type="javax.sql.DataSource".
Use Tomcat's older JDBC pool with factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" and type="org.apache.tomcat.jdbc.pool.DataSource".
I don't know enough about the differences to give any strong recommendations re. using one over the other.
The Default DBCP 2 Tomcat Pool
To make use of the default Tomcat DB connection pool, via a JNDI resource, you should follow the instructions provided on this Tomcat documentation page: JNDI Datasource How-To
Specifically, you need to use type="javax.sql.DataSource".
Here is a very basic resource configuration - not suitable for production, but useful to illustrate the approach. And it's for MySQL not Oracle, so there will be some adjustments needed:
<Resource name="jdbc/my_db"
auth="Container"
type="javax.sql.DataSource"
initialSize="5"
username="db_user"
password="***"
driverClassName="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mytestdb" />
When using initialSize="5", I saw the 5 expected connections on the DB server.
In the JVM, a connection pool was created - as shown in this VisualVM heap dump:
Here we can see that the pool is created, it has 5 connection objects, and it is using Tomcat DBCP - which, in turn, is a fork of Apache Commons DBCP 2.
DBCP 2 provides support for JDBC 4.1.
Tomcat's Home-Grown JDBC Pool
If you look at the Tomcat documentation on this other page - The Tomcat JDBC Connection Pool - you will see different guidance.
Specifically it states that you need both of the following in your <Resource>:
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
type="org.apache.tomcat.jdbc.pool.DataSource"
If you use this, you will also be using a DB connection pool - but it will be based on the older Tomcat JDBC pool package:
In this example, I used initialSize="3".
In the Tomcat documentation this is presented as "a new connection pool" (it was a replacement for the previously used Commons DBCP 1.x). It's not as new as the Tomcat default DBCP 2 solution. I think the documentation wording is out-of-date, now. And somewhat confusing, because of that.
Additional Notes
Notes from a member of the Tomcat commit team (see here):
Tomcat JDBC is Tomcat's "home grown" database connection pooling and does not use poolPreparedStatements
Tomcat DBCP is Tomcat's package renamed fork of Apache Commons DBCP 2.
Tomcat DBCP is used by default.
You can always choose to implement a pool directly in your code, without using a JNDI <Resource> annotation. There are many available options.
(In my initial tests, there was a problem with my Tomcat installation which resulted in the creation of too many connections for DBCP 2. That misled me, initially).

Oracle Universal Connection Pool (UCP) is a Java connection pool that you can use as well. It is feature rich and works well with Oracle RAC, DG etc., Check out UCPServlet for an example.

Related

Failover Settings for Oracle JDBC Driver

Currently we are using and have configured Oracle using the JDBC Driver.
We configure the org.apache.commons.dbcp.BasicDataSource bean.
But how can failover parameters like ONSConfiguration and FastConnectionFailover be configured?
Is this the right way?
<property name="connectionProperties" value="ONSConfiguration=adczatdb01:6200,adczatdb02:6200,slcc17adm01:6200,slcc17adm02:6200" />
You need to use Universal Connection Pool (ucp.jar) to use Fast Connection Failover (FCF) along with the JDBC driver (ojdbc8.jar). Also, the UCP version that you are using is important too. Check out the whitepaper for more details on how these connection properties are set. Also, check out UCP developer's guide.

Apache Ignite JDBC driver - JDBC Connection Pool options

I'm currently trying to set-up Apache Ignite with C3P0 as my JDBC Connection pool, but I noticed that since the Ignite driver doesn't support transactions, C3P0's not usable.
Has anyone had any luck getting a JDBC connection pool going with the Ignite driver? Suggestions?
EDIT:
Updating with exactly why C3P0 doesn't work with Ignite's JDBC Driver
So take a look at this line of code
To create a new pooled connection, C3P0 attempts to set transaction isolation through the connection/driver.
That eventually leads us to this line of code in the Ignite driver, which basically tells us that the Ignite driver doesn't support SQL transactions.
Ignite itself DOES support transactions as specified here but it appears the JDBC implementation does not.
So I need an alternative to C3P0 if I want to set up a JDBC connection pool; any suggestions?
It turns out the JDBC driver for Apache Ignite isn't currently JDBC compliant. Specifically the part that breaks it is that it doesn't have transaction support. As a result, your typical JDBC-pool implementation won't work with the Ignite Driver
There's now a ticket for this here: https://issues.apache.org/jira/browse/IGNITE-4191
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("org.apache.ignite.IgniteJdbcDriver");
ds.setUrl("jdbc:ignite:cfg://cache=default#file:///the/path/to/ignite-config.xml");
ds.setInitialSize(2);
ds.setMinIdle(2);
Try BasicDataSource http://commons.apache.org/proper/commons-dbcp/configuration.html

Configuring resource in Tomcat's context.xml to access remote Weblogic JMS queues

What I intend to do is access remote queues in Oracle Weblogic JMS (version 10.3.4) from a spring application deployed in Tomcat7.
For this I am trying to configure a Resource (eg JMS connection factory, queues etc) in Tomcat's context.xml file. Then access this resource using jndi lookup in the spring configuration file and provide it to the necessary beans. I have already created connection factory and queues in Weblogic JMS and they can be accessed using jndi names.
I am able to make it work successfully when using ActiveMQ instead of Weblogic JMS. However with Weblogic JMS, I am facing an issue with configuring the Resource element. I am not sure what attributes to be used with Resource tag while connecting to Oracle Weblogic JMS.
When working with ActiveMQ the resource element config looks like below
<Resource name="jms/MyConnectionFactory" auth="Container"
type="org.apache.activemq.ActiveMQConnectionFactory"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
description="JMS Queue Connection Factory"
brokerURL="tcp://localhost:61616" brokerName="MyActiveMqBroker"/>
I am struggling to find the configuration to be used with Oracle Weblogic JMS. I have gone through documentations to see how to do it but with no luck.
Any help or pointers would be highly appreciated.
Thanks.

MysqlConnectionPoolDataSource or c3p0 like library?

What's the difference between MysqlConnectionPoolDataSource and C3p0, BoneCP or dbcp library for connection pooling? I don't understand why use a library if mysql connector give connection pooling.
A ConnectionPoolDataSource is not a connection pool (or at least: it shouldn't be), it is intended to be used by a DataSource that provides pooling (eg from an application server). A ConnectionPoolDataSource provides the physical connections that will be held in the connection pool. Besides creating those physical connections a ConnectionPoolDataSource shouldn't do anything else.
So if you are working in an application server, use the pooling provided by the DataSources of the application server. If you are in a standalone application or a server that doesn't provide datasources on its own, use third party connection pools like BoneCP, c3p0 or Apache DBCP. If MySQL also provides a normal DataSource that provides pooling, then you could use that.

Hot deploy problem in Tomcat with proxool and oracle

I have a problem concerning with proxool and oracle driver in Tomcat.
The web application I use contains a webservice jar file using metro and a servlet to initialize / start the proxool pool. The proxool pool is configured with an oracle connection. When the service is called, it fetches a connection from the pool, executes a statement and returns. I close the resultset, the statement and the connection in the service method afterwards.
When I now try to hot undeploy the web application, the servlet stops proxool pool by ProxoolFacade.shutdown(); in it's destroy method.
The problem is, that the hot undeploy can not finish because the Oracle driver ojdbc5.jar can not be deleted from the folder of the extracted web application.
Trying the same with a SQL server database and the jtds driver it works without this problem.
Used versions:
Apache Tomcat 6.0.18
Oracle 11g JDBC driver 11.1.0.6.0
Proxool 0.9.1
Anyone has an idea?
Regards Timo
You must add the Oracle driver to the Tomcat instance and configure Tomcat JNDI with an Oracle datasoure.
This way, the driver will stay alive when the app is terminated and Tomcat will control the pooling of the DB connections. The reason for your problem is that some DB drivers "hook" into the VM. This means that Tomcat can't unload the classes during redeploy -- there are still references around.
I'd even suggest to try to move proxool into the Tomcat server and have Tomcat manage the pools for you. That would make hot deploy much faster and more reliable, even if you leak connections.

Resources