views:

42

answers:

2

From the examples that I have read and from the documentation on MysqlConnectionPoolDataSource at http://www.control.auc.dk/~04gr733/filer/javadoc-DB-driver/com/mysql/jdbc/jdbc2/optional/MysqlConnectionPoolDataSource.html it seems that Java SE applications cannot use connection pooling. Is this true? Can I get connection pooling to work for MySql?

Does the same go for PreparedStatement pooling?

UPDATE: I came across the following from the MySQL website with regards to their connection pooling.

    /*
     * Create a JNDI Initial context to be able to
     *  lookup  the DataSource
     *
     * In production-level code, this should be cached as
     * an instance or static variable, as it can
     * be quite expensive to create a JNDI context.
     *
     * Note: This code only works when you are using servlets
     * or EJBs in a J2EE application server. If you are
     * using connection pooling in standalone Java code, you
     * will have to create/configure datasources using whatever
     * mechanisms your particular connection pooling library
     * provides.
     */

This is what has made me look at a way to get this right for a Java SE application. But re-re-reading it, it seems to me that they are making reference to the manner in which you obtain a reference to the datasource. Could I not use a dependency injection framework or manual insert a dependency on the datasource for my DAO object? I am going to give that a try.

+1  A: 

The MysqlConnectionPoolDataSource won't do it itself, use something like c3p0 for that, or look at the examples at the SDN on Connection Pooling.

Konerak
+1  A: 

You could use some third party Connection Pool for JDBC such as:
BoneCP , C3P0 or DBCP

Romain Hippeau
@Romain, so that means that I have to roll my own?
uriDium
@uriDium - I would not roll my own, but use a preexisting one. In the past using DB/2 I have instantiated a DataSource object and then called the getConnection from there and it worked just fine (same as what an application container does)
Romain Hippeau