users@glassfish.java.net

Re: Hibernate + ConnectionPool

From: Craig Ringer <craig_at_postnewspapers.com.au>
Date: Tue, 24 Aug 2010 09:56:24 +0800

On 08/24/2010 12:22 AM, glassfish_at_javadesktop.org wrote:
> Hi all,
> I have setup a connectionpool in glassfish server. What are the steps
> needed to make my hibernate enabled application to use that
> connectionpool? does it have to be Java Web application? the reason
> i'm asking this because i have a Java Web project started in netbeans
> but all i have is a bean class for hibernate and another class with
> main.

Configure a JDBC Resource that uses that connection pool in Glassfish's
admin console.

In your META-INF/persistence.xml, use <jta-data-source/> to tell the
provider where to find the pool. For example, here's mine, declaring
that I want to use a pool named "jdbc/classads-database":

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
   <persistence-unit name="classads-PU" transaction-type="JTA">
     <provider>org.hibernate.ejb.HibernatePersistence</provider>
     <jta-data-source>jdbc/classads-database</jta-data-source>
     <class>com.mycompany.SomeEntityClass</class>
     <class>.....</class>
     <exclude-unlisted-classes>true</exclude-unlisted-classes>
     <properties/>
   </persistence-unit>
</persistence>

The key part is the <jta-data-source/> tag.

You should probably also declare that you require that resource in your
web.xml deployment descriptor, but it's not critical to do so.
Netbeans's web.xml editor will make that trivial for you.

Personally I found it useful to configure a setup/sun-resources.xml
resource descriptor that permits automatic creation of the connection
pool and jdbc resource when the application is deployed. Here's the one
I'm using in the same project as the above persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//Sun Microsystems, Inc.//DTD Application
Server 9.0 Resource Definitions //EN"
"http://www.sun.com/software/appserver/dtds/sun-resources_1_3.dtd">
<resources>
     <jdbc-connection-pool
         name="classads-database-pool"
         datasource-classname="org.postgresql.ds.PGSimpleDataSource"
         res-type="javax.sql.DataSource" max-pool-size="8"
         steady-pool-size="2">
             <property name="User" value="XXXXX"/>
             <property name="Password" value="XXXXXX"/>
             <property name="databaseName" value="classads"/>
     </jdbc-connection-pool>
     <jdbc-resource enabled="true" jndi-name="jdbc/classads-database"
           object-type="user" pool-name="classads-database-pool"/>
</resources>

(I've had a hard time finding decent documentation on sun-resources.xml,
but the DTD is certainly helpful).

By the way, you can create a persistence.xml file using Netbeans if you
prefer, after configuring the data source in Netbeans services. It'll
also help you out by auto-generating the entity classes, though they're
really only a starting point for you rather than really usable entity
classes. In particular, I find I tend to have to remove most of the
bidirectional associations, add orphanRemoval=true where appropriate,
change the type on some fields, etc.

--
Craig Ringer