Mentawai internally supports three different connection pools: DBCP, C3P0 and BoneCP. We recommend BoneCP.
Setting up in the application manager:
@Override public ConnectionHandler createConnectionHandler() { Props props = getProps(); String driver = props.getString("jdbc.driver"); String url = props.getString("jdbc.url"); String user = props.getString("jdbc.user"); String pass = props.getString("jdbc.pass"); return new BoneCPConnectionHandler(driver, url, user, pass); }
Using it:
The connection pool is fully integrated with the IoC container so you can get a connection from anywhere. You also need not worry about returning the connection to the pool because this is done automatically by the container in the thread local scope.
// inside the action you can just do: Connection conn = (Connection) input.getValue("conn");
DI and Auto-wiring:
The connection will be injected automatically in any component that depends on it, by setter or constructor. For example, to write a DAO that depends on the connection all you have to do is include a connection parameter in the constructor:
public class JdbcUserDAO implements UserDAO { private final String blah; private final Connection conn; private final int foo; public JdbcUserDAO(String blah, Connection conn, int foo) { this.blah = blah; this.conn = conn; this.foo = foo; } }
Tuning the connection pool:
The BoneCPConnectionHandler implementation has a getBoneCP() method that returns the BoneCP object. With that object you can configure the BoneCP pool in any way you want. All ConnectionHandler implementations externalize their underlying pool that way in case you need direct access for configuration. Mentawai already configures the pools with their standard and recommended options for general applications, so you should not need to configure anything else.