How to use connection pool with java,MySQL and Tomcat 6 - java

How can I use Connection pool in Java+MySQL+Tomcat 6?
I've read this article http://dev.mysql.com/tech-resources/articles/connection_pooling_with_connectorj.html but it is still not quite clear for me.
Where do I use the Connector/J? Do I put it in a static variable? Do I use a SessionListener? Does it need any configuration?
Thank you in advance!

You should read the Tomcat 6 JNDI document. Look for the "JDBC Data Sources" section and it will tell you everything you need to know about pooling connections with Tomcat.

You can easily implement MySQL connection pooling in Java by using Java's GenericObjectPool that provides robust pooling functionality for arbitrary objects.
See detailed example in this post: How to set up a MySQL connection pool in Java

Related

flexypool for DB2 and Oracle Datasources

We uses the below connection pools in our application
1) DB2DataSource 2) OracleDataSource
Can we implement flexypool metrics and failover strategies to the above connection pools? I didn't find any specific doc or example code related to it
under vladmihalcea's flexypool github repository.
Could you please suggest on this?
Sure, you can. You just need to use a connection pool like HikaricP in front of these two DataSources, and you'll get all the functionality offered by FlexyPool.

Alternate of setAccessToUnderlyingConnectionAllowed method in c3p0

I am using org.apache.commons.dbcp.BasicDataSource in my hibernate for connection pooling. and i also used the below method :
basicDataSource.setAccessToUnderlyingConnectionAllowed(true);
Now i want to use the c3p0 connection pooling. and i am trying to use above method but it is not available in ComboPooledDataSource class. so anyone can help me to give me the alternative of this method.
If you need to access the underlying Connection in c3p0:
use raw Connection operations, see http://www.mchange.com/projects/c3p0/#raw_connection_ops
if you are using JDBC4 and a c3p0-0.9.5-pre release (-pre8 is production quality I think, just a few loose ends left to clean up), then you can use the standard JDBC4 unwrap method.
You don't need to set that kind of method for C3P0. Hibernate allows you to use:
an internally wired C3P0
or an external supplied C3P0 DataSource
Either way, all connections are going to be monitored by the C3P0 internal guards and Hibernate works seamlessly with any of those two alternatives.

Glassfish JDBC: Do I have to use only jdbc/__default?

I try to use Glassfish/MySQL. I have created JDBC resource and JDBC connection pool for MySQL.
But if I tried to put MySQL JDBC resource in jta-data-source, nothing works.
Then if I tried to modify jdbc/__default and change its connection pool from DerbyPool to MySQL, it works. My entity gets persists to the correct table.
So do I have to use jdbc/__default only as my JDBC resource for my app? How can I use the JDBC resource and JDBC connection pool I created in my app?
I really have hard time understanding how to use JDBC in Glassfish.
This is my first time to ask question in this forum. Thank you very much.
See this link for a step by step tutorial on how to create JDBC connection pools in Glassfish server. Here is the official documentation on how to do it. Or you can read this SO question. And another resource that you can use is this SO question.

Client-side DataSource in place of DriverManager

In Java, I use DriverManager for client side code and Connection pooled DataSource for server side code. Is this the standard way to do it or is it possible to use a separate DataSource on the client side as well?
I'm assuming you want to know if you can use a connection pool outside of a server application.
You can, either you to write your own pool implementation, which is going to be tedious... or you can use a library like commons dbcp or c3po.
Hope this helps

Is there any performance difference between these two JDBC Connectivity?

I have used MySqlDataSource for in jdbc connectivity.I have used following code
MysqlDataSource d = new MysqlDataSource();
d.setUser("user");
d.setPassword("pass");
d.setServerName("hostname.com");
d.setDatabaseName("db");
Connection c = d.getConnection();
Also i have searched there is an option of Configuring a MySQL Datasource in Apache Tomcat.
Is there any performance difference between these two? which one is best to use?
Configuring Datasource in tomcat will help you to share same data source between applications running in same tomcat. that Datasource will be managed by container (tomcat in your case).
while the Datasource created in code will be created by your application and can be used by that application only.
So if you have multiple application running on tomcat and accessing same data source, that configuring Datasource in tomcat will be good approach and have performance factor because only one data source is created and not having separate connections for each application
But if you have only single application that the first approach you have used is good one
They both use the internally the same driver, i dont think the performance is much different here, i guess if you need to access teh database only at that place and the enduser isn't supposed to use his own authentication you may use it directly from java, but if you will need the connectivity on different places it could be helpful to configure this using apache configuration, specially that if anything changes like database server, user name or whatever you don't need to get in the code to change it, this could be very important if end users have to set their own configurations.
The improvement of configuring a pool of Connections (as the one provided by tomcat) is mainly that you will actually create and close a lot less of connections.
When using a pool, when you request a Connection to a pool it will look if it has any connection already created and available for reuse and, if it has, it will provide you with it (instead of creating a new Connection, which is a heavy operation). You must still close() a Connection provided by Tomcat so Tomcat knows that it can now reuse when it is requested again.
Additionally, the advantage of the pool is that your code does not need to know the configuration data for the Connection. He just requests a Connection from a given pool and the sysadmin configures it, allowing for greater flexibility (the sysadmin does not need to know how to configure your app, just how to configure the Tomcat which is fairly more standard).

Categories

Resources