Hibernate
Resin 3.1

Documentation
Examples
Changes

Overview
Installation
Configuration
Quercus
SOA/IoC
JSP
Servlets and Filters
Admin (JMX)
EJB
Amber
Security
Performance
Hessian
XML and XSLT
Third-party
Troubleshooting/FAQ

IDE's
Scottit
Groovy
SwiftMQ
OpenJMS
Hibernate
Database
OpenJMS
Third-party
Database

Hibernate

Hibernate now provides a JCA interface in its beta release. To configure it, use the following:

web.xml
<web-app xmlns="http://caucho.com/ns/resin">
  <resource jndi-name="java:comp/env/hibernate">
    <type>net.sf.hibernate.jca.ManagedConnectionFactoryImpl</type>
    <init>
      <dialect>net.sf.hibernate.sql.MySQLDialect</dialect>
      <driver-class>org.gjt.mm.mysql.Driver</driver-class>
      <connection-url>jdbc:mysql://localhost:3306/test</connection-url>
    </init>
  </resource>
</web-app>

Unfortunately, Hibernate's JCA interface does not yet allow configuration of a JDBC DataSource, so you can't yet take advantage of Resin's pooling.

Using the Hibernate session factory uses a similar pattern to the JDBC DataSource. Like JDBC Connections, you must close the Hibernate session in a finally block.

TestServlet
package example;

import java.io.*;

import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Session;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequest;

import javax.naming.Context;
import javax.naming.InitialContext;

public class TestServlet extends HttpServlet {
  private SessionFactory _hibernateFactory;

  /**
   * Load and cache the hibernate factory at startup.
   */
  public void init()
    throws ServletException
  {
    try {
      Context ic = new InitialContext();
      _hibernateFactory = ic.lookup("java:comp/env/hibernate");
    } catch (Exception e) {
      throw new ServletException(e);
    }
  }

  public void service(...)
    throws ServletException
  {
    net.sf.hibernate.Session hSession;

   
    try {
      hSession = _hibernateFactory.openSession();
  
      ...
    } finally {
      hSession.close();
    }
  }
}

OpenJMS
Third-party
Database
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin ® is a registered trademark, and Quercustm, Ambertm, and Hessiantm are trademarks of Caucho Technology.