Failover Settings for Oracle JDBC Driver - oracle

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.

Related

How do I pool Oracle connections in Tomcat?

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.

Does Hikari Connection need test connection when starts the application?

I configured Hikari following this tutorial: https://www.baeldung.com/spring-boot-hikari
When Spring Boot starts Hikari starts too, as follows in the next image:
Is it possible to configure Hikari to start when the first ReST request is being received?
Quoted from Hikari's official document on Github:
connectionTestQuery
If your driver supports JDBC4 we strongly recommend not setting this property. This is for "legacy" drivers that do not support the JDBC4
Connection.isValid() API. This is the query that will be executed just
before a connection is given to you from the pool to validate that the
connection to the database is still alive. Again, try running the pool
without this property, HikariCP will log an error if your driver is
not JDBC4 compliant to let you know. Default: none
As documentation:
"For a pooling DataSource to be created, we need to be able to verify that a valid Driver class is available, so we check for that before doing anything. In other words, if you set spring.datasource.driver-class-name=com.mysql.jdbc.Driver, then that class has to be loadable."(https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/htmlsingle/)

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

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.

number of live connections

My application is using Websphere 6.1, Spring JDBC and oracle.I am using connection pooling to manage the connections.Is there any way to find out the number of connections active(alive) between the application and database at any point of time?.Can we have any indicator to let us know when a connection is/was dropped?
One option would be to manage your connection pool via JMX. Spring has excellent support for it. You just need to expose your connection pool bean via org.springframework.jmx.export.MBeanExporter. You can pick and choose the methods you want to expose. For example if you use DBCP you can use the method BasicDataSource#getNumActive().

Resources