Connection pool with javax MySQL - java

Is there any way to make a Connection pool using this only:
http://docs.oracle.com/javase/7/docs/api/javax/sql/package-summary.html ?
In tomcat7 and MySQL i have a lot of problems with DBCP driver.

I believe you can't. You could implement your own ConnectionPool but this is a realy bad idea.
My suggestion is to use some 3rd party library like c3p0. It is very easy to use.
c3p0 example
Other ConnectionPool Libraries:
JavaXT Connection Pool
Proxool
The Tomcat JDBC Connection Pool + howto
HikariCP

In theory you should be able to write your own Connection Pool Manager by providing your own implementations of the interfaces listed in the interfaces section.
"The connection pool manager, a facility in the middle tier of a three-tier architecture, uses these classes and interfaces behind the scenes. "
In practice, why? perhaps you need to explain your statement "In tomcat7 and MySQL i have a lot of problems with DBCP driver. " because that is leading you down a rabbit hole of duplicated effort.

Related

DataSource and DriverManager on J2SE

I'm interested in developing a desktop application that connects to a MySQL DB.
After reading this java tutorial on DB connection (and several others) I have a question.
Using DataSource seems popular on J2EE while DriverManager is a common choise for desktop applications.
Is it still possible to use DataSource on a desktop application? If yes, is it a better choice over DriverManager?
It's perfectly possible to use a DataSource on a desktop application.
Server apps are usually multi-thread, multi-user applications, where several connections to the database are open in parallel. A pooled DataSource is critical here.
Desktop apps are usually single-user applications, where you just need one connection to the database. So a pooled datasource isn't necessary in this case.
I'll quote the javadoc:
An alternative to the DriverManager facility, a DataSource object is the preferred means of getting a connection.
Normally you use DriverManager when you just want a connection for one time while with DataSource you get other features such as connection pooling and distributed transactions.
Hope this helps.

how to manage connections to dynamically created databases

I need to manage connections to multiple databases in my web app. following are facts regarding the current implementation:
1- I use Tomcat
2- databases are created dynamically at runtime ( i am using mysql)
without a doubt, having a connection pool to manage database connections is optimal.
Since the databases are not known at the start of the application, it was not possible for me to set up datasources and make connection pools. (I could not find a way in Tomcat to make dynamic connection pool: a connection pool that is created at runtime).
my question is: what other options do I have to work efficiently with connections to multiple databases ? (I don't have experience to implement connection pools myself)
is there any library that can be used with tomcat and allow me to establish multiple connection pools to different databases at runtime ? if not what do you suggest that I do instead of connection pools ?
i am fairly new with this issue therefore please correct and guide me if I am messing up concepts.
Thank you in advance.
The MySQL JDBC driver allows omitting the database name from the connection URL as follows:
jdbc:mysql://localhost:3306
You only need to specify the database by Connection#setCatalog() or directly in the SQL queries. See also its reference documentation:
If the database is not specified, the connection will be made with no default database. In this case, you will need to either call the setCatalog() method on the Connection instance or fully specify table names using the database name (that is, SELECT dbname.tablename.colname FROM dbname.tablename...) in your SQL. Not specifying the database to use upon connection is generally only useful when building tools that work with multiple databases, such as GUI database managers.
This allows you for creating a single and reuseable connection pooled datasource in Tomcat. You'll perhaps only need to rewrite your connection manager and/or SQL queries.
There are enough connection pooling framework in the open. Proxool is definitely among the best. Its pretty flexible and easy to use.

Object Pooling managment algorithm

Anyone can help me to write an algorithm for object pooling. I am very new in J2EE and i need object pooling for database connection. So please help out.
Thanks
Database connection pooling is a tough problem and is difficult to get it right. However, you could use several open source solutions which offer Connection Pooling. You can consider using
C3P0 or Apache DBCP to get the connection pooling you desire.
In case you are working in an application server environment such as Glassfish, Weblogic or Jboss, connection pooling is provided by the application server itself. You need to create a datasource and enable pooling that you wish to have.
Rather than writing your own Object pool you should probably use an existing solution, e.g.
Commons Pool: http://commons.apache.org/pool (for object pools in general)
or
Commons DBCP: http://commons.apache.org/dbcp (for db connection pools)
All of the major Servlet and Java EE containers come with their own Connection pooling implementations, available through JNDI. Check the documentation with your container.
I am very new in J2EE and i need
object pooling for database
connection.
The whole point of the Java EE platform is to relief the developper form writing such infrastructure code, and focus on the business logic. Whether the platform succeeds at this or not, is another debate, but it's a least the vision.
Given that you are new to it, I would strongly suggest that you spend some time to get an better understanding of what the platform provide, and what's the vision behind it.
Connection pooling is just one thing, the platform provide specific ways to deal with configuration, security, deployment, monitoring, etc.
Regarding connection pooling specifically, see What is best approach for connection pooling?

What's the difference between Tomcat JNDI based and Spring/Hibernate DS based connection pooling

I have been looking into connection pool options and it is somewhat unclear to me what the differences in Tomcat JNDI connection pool approach is, compared to the Spring/Hibernate solution to the same.
Whilst it's possible to achieve the pooling using either 1, 2, the specific application we have would lend itself better to us using Tomcat given the constraints we have.
Reading about, there is some suggestion to just stick with Spring/Hibernate.
Are there any notable differences worth mentioning between each approach? What are other's personal experience of one or the other (or both) - I have successfully been using Spring/Hibernate for years now.
The two approaches are complementary, not mutually exclusive. In production systems, the likes of Spring/Hibernate will obtain a reference to the connection pool from the appserver, in the form of a javax.sql.DataSource, usually by looking for it on the JNDI tree. It generally considered to be the appserver's "job" to manage the connection pool and its connections.
Remember, JNDI is just a place for registering objects for sharing, it does in itself mandate any given connection pool mechanism. The app server creates and configures the pool, and the applications (via Spring/Hibernate/whatever) use it.
It's just as valid, however, for the applications to configure and manage the connection pool themselves. This does mean a bit more work for the application, though, with less reliance on the appserver.

Is DBCP (Apache Commons Database Connection Pooling) still relevant?

The JDBC 3.0 spec talks about Connection (and Prepared Statement) pooling.
We have several standalone Java programs (i.e. we are not using an application server) that have been using DBCP to provide connection pooling. Should we continue to use DBCP, or can we take advantage of the JDBC-provided pooling and get rid of DBCP?
We are using MySQL (Connector/J) and will eventually be adding SQL Server support (jTDS); it's unlikely that we'll support any other databases.
EDIT: See comment below about my attempt to eliminate the connection pooling library. It appears that DBCP is still relevant (note that some commenters recommended C3P0 over DBCP).
Based on the encouragement of other posters, I attempted to eliminate DBCP and use the MySQL JDBC driver directly (Connector/J 5.0.4). I was unable to do so.
It appears that while the driver does provide a foundation for pooling, it does not provide the most important thing: an actual pool (the source code came in handy for this). It is left up to the application server to provide this part.
I took another look at the JDBC 3.0 documentation (I have a printed copy of something labeled "Chapter 11 Connection Pooling", not sure exactly where it came from) and I can see that the MySQL driver is following the JDBC doc.
When I look at DBCP, this decision starts to make sense. Good pool management provides many options. For example, when do you purge unused connection? which connections do you purge? is there a hard or soft limit on the max number of connections in the pool? should you test a connection for "liveness" before giving it to a caller? etc.
Summary: if you're doing a standalone Java application, you need to use a connection pooling library. Connection pooling libraries are still relevant.
DBCP has serious flaws. I don't think it's appropriate for a production application, especially when so many drivers support pooling in their DataSource natively.
The straw that broke the camel's back, in my case, was when I found that the entire pool was locked the whole time a new connection attempt is made to the database. So, if something happens to your database that results in slow connections or timeouts, other threads are blocked when they try to return a connection to the pool—even though they are done using a database.
Pools are meant to improve performance, not degrade it. DBCP is naive, complicated, and outdated.
I prefer using dbcp or c3p0 because they are vendor neutral. I found out, at least with mysql or oracle, that whenever I try to do something with the jdbc client that is not standard sql I have to introduce compile-time dependency on the vendor's classes. See, for example, a very annoying example here.
I am not sure about mysql, but oracle uses their specific, non-standard classes for connection pooling.
People still use DBCP, I think it even comes as a default with Hibernate.
Is DBCP not meeting your current needs?
I'm not a big believer in replacing infrastructure unless there's already a performance or functionality gap that it can't fill, even if there are newer or fancier alternatives around.

Categories