I wish to use put connections with Java (since it is pricey to produce one connection per thread) so I am while using MysqlConnectionPoolDataSource() object. I am persisting my databases across threads. So, I am only one datasource through the application such as this:

  startRegistry();    // creates an RMI registry for MySQL
  MysqlConnectionPoolDataSource dataSource = new MysqlConnectionPoolDataSource();

  InitialContext context = createContext();   // Creates a context
  context.rebind("MySQLDS", dataSource);

Since I've my datasource produced, I am doing the next in every separate thread:

  PooledConnection connect = dataSource.getPooledConnection();
  Connection sqlConnection = connect.getConnection();

  Statement state = sqlConnection.createStatement();

  ResultSet result = state.executeQuery("select * from someTable");
  // Continue processing results

I you know what I am confused on may be the call to dataSource.getPooledConnection();
Is really fetching a put connection? And it is this thread safe? I observed that PooledConnection has techniques like inform() and wait()... and therefore I do not believe it is doing what It does...

Also, when and just how must i release the bond?

I am wondering if it might be more advantageous to roll my very own because then I'd become more acquainted with everything, however i don't actually want to reinvent the wheel within this situation :).

Thanks SO

This isn't the proper way. The datasource must be handled by whatever container you are running the applying in. The MysqlConnectionPoolDataSource is not an association pool. It's really a concrete implementation from the [cde] interface. You normally define it within the JNDI context and acquire it after that. Also MySQL itself states everything clearly in their documentation.

Now, using it is dependent on the objective of the applying. If it's an internet application, you will want to touch on the JNDI assets documentation from the servletcontainer/appserver under consideration. If it's for instance Tomcat, you'll be able to think it is here. If you are managing a client application --that I'd highly question the need for an association pool--, you will want to search for an association pooling framework which could take advantage from the MySQL-provided connection put datasource, for example C3P0.

Another trouble with the code that you simply published would be that the javax.sql.DataSource will return the underlying connection that is thus not a put connection. Calling close onto it will not return the bond towards the pool, but simply really close it. The pool needs to produce a new connection each time.

Then your threadsafety story, that is dependent around the real connection pooling framework under consideration. C3P0 has proven its robustness in a long time, you do not be worried about it as being lengthy while you write JDBC code according the conventional idiom, i.e. use only the JDBC connects and get and close all assets (PooledConnection#getConnection(), Connection and Statement) in least possible scope.