Providing Connections for Hibernate
Hibernate, being an Object->Relational DB
Mapper for Java, needs access to JDBC connections. Out of the box, Hibernate is
fairly self contained when it comes to connection control. By default,
Hibernate ships with the ability to obtain a data source implementation (
javax.sql.DataSource
) from JNDI by setting the properties
appropriately. You can find different kind of Hibernate connection pool libraries here
hibernate.connection.datasource
= java:/comp/env/jdbc/test
Alternatively, if JNDI isn't an option, you can
use a Hibernate-internal connection pool implementation (C3PO), and simply give
driver/url information for Hibernate to create and pool its own connections:
hibernate.connection.driver_class = org.postgresql.Driver
hibernate.connection.url = jdbc: postgresql://localhost/test
hibernate.connection.username = root
hibernate.connection.password = password12
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
In both of these cases, it is the same process to get a Hibernate
Session
Session session = sessionFactory.openSession();
If you
need to give Hibernate connections to use on a per-session basis, you
can always ask the session factory for a session running on a particular connection
Connection conn = // ... get connection from some outside source.
Session session = sessionFactory.openSession(conn);
If you have legacy connection pooling or non-JNDI data sources
that you need to get to, you can simply create an implementation of the Hibernate
org.hibernate.connection. ConnectionProvider interface.
The ConnectionProvider API is
fairly straightforward, in consists of four methods:
- configure(Properties) - This method
tells the connection provider to prepare itself using the Hibernate
properties passed in.
- getConnection() throws SQLException - Get a method
from your connection source for hibernate to use.
- closeConnection(Connection) throws SQLException - Release a
connection Hibernate has been using (potentilly putting it back in to your
legacy pool, or whatever else).
- close() - This method tells the connection
provider to release any resources for shutdown.
Once implemented, it is simply a matter
of telling Hibernate at configuration-time that you want it to use your
connection provider:
hibernate.connection.provider_class=com.javalobby.tnt.MyConnectionProvider
There are a couple of
solutions, all of them based in static fields - straight static references,
singletons, and thread-locals. It largely depends on your implementation what
will work for you - I'll just address static references as they are the most
simple, and most straightforward
// configuring hibernate here.
Configuration configuration = new Configuration()
.setProperty(Environment.CONNECTION_PROVIDER, "com.hibernate.test.MyConnectionProvider");
// providing reference to connection pool here.
LegacyConnectionPool legacyPool = getLegacyConnectionPool();
com.javalobby.tnt.MyConnectionProvider.connectionPool = legacyPool;
// Get the session factory
// (this will invoke the construction of the connection provider, etc)
SessionFactory sessionFactory = configuration.buildSessionFactory();
Here is the implementation of the connection provider:
package com.javalobby.tnt;
import java.sql.*;
import java.util.*;
import org.hibernate.*;
import org.hibernate.connection.*;
public class MyConnectionProvider implements ConnectionProvider {
public static LegacyConnectionPool connectionPool;
public void configure(Properties props) throws HibernateException {
if(connectionPool == null) {
throw new HibernateException("Connection pool must be set.");
}
public Connection getConnection() throws SQLException {
return connectionPool.getConnection();
}
public void closeConnection(Connection conn) throws SQLException {
connectionPool.freeConnection(conn);
}
public void close() {
}
}
Comments
Post a Comment