Updated HibernateUtil

git-svn-id: https://svn.jboss.org/repos/hibernate/trunk/Hibernate3/doc@8152 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Christian Bauer 2005-09-12 14:52:21 +00:00
parent 57ba6ab422
commit 0fa02198d8
4 changed files with 29 additions and 27 deletions

View File

@ -1602,16 +1602,15 @@ hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect]]></programlisting>
</sect2> </sect2>
<sect2 id="configuration-j2ee-currentsession" revision="1"> <sect2 id="configuration-j2ee-currentsession" revision="2">
<title>Automatic JTA and Session binding</title> <title>Automatic JTA and Session binding</title>
<para> <para>
For non-managed environments we suggested <literal>HibernateUtil</literal> with We recommend that you bind the <literal>SessionFactory</literal> to JNDI in
a static <literal>SessionFactory</literal>, and <literal>ThreadLocal</literal> a managend environment. For transaction and <literal>Session</literal> handling,
management of the Hibernate <literal>Session</literal>. This approach isn't easy you could use the already introduced <literal>HibernateUtil</literal> helper class.
to use in an EJB environment, as several EJB's may execute inside the same However, EJBs might not execute in the same thread, which makes <literal>ThreadLocal</literal>
transaction but not the same thread. We recommend that you bind the handling not always appropriate (e.g. when two session beans call each other).
<literal>SessionFactory</literal> to JNDI in a managend environment.
</para> </para>
<para> <para>
@ -1623,7 +1622,11 @@ hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect]]></programlisting>
and <literal>hibernate.transaction.auto_close_session</literal> configuration option, and <literal>hibernate.transaction.auto_close_session</literal> configuration option,
will be set automatically for every <literal>Session</literal> you retrieve with will be set automatically for every <literal>Session</literal> you retrieve with
<literal>getCurrentSession()</literal>, so they will also be flushed and closed <literal>getCurrentSession()</literal>, so they will also be flushed and closed
automatically when the container ends the JTA transactions. automatically when the container ends the JTA transactions. This is an alternative
to <literal>ThreadLocal</literal> management. The <literal>HibernateUtil</literal>
class you can find with the <emphasis>CaveatEmptor</emphasis> application can
actually switch between both strategies automatically, hence, keeps your code
portable between transaction-local, BMT, and CMT environments.
</para> </para>
<para> <para>

View File

@ -442,7 +442,7 @@ Indexes: cat_pkey primary key btree (cat_id)]]></programlisting>
</sect1> </sect1>
<sect1 id="quickstart-playingwithcats" revision="2"> <sect1 id="quickstart-playingwithcats" revision="3">
<title>Playing with cats</title> <title>Playing with cats</title>
<para> <para>
@ -503,7 +503,7 @@ public class HibernateUtil {
public static final ThreadLocal session = new ThreadLocal(); public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() { public static Session getCurrentSession() {
Session s = (Session) session.get(); Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet // Open a new Session, if this Thread has none yet
if (s == null) { if (s == null) {
@ -539,7 +539,7 @@ public class HibernateUtil {
<literal>process()</literal> method might look like this (sans exception handling): <literal>process()</literal> method might look like this (sans exception handling):
</para> </para>
<programlisting><![CDATA[Session session = HibernateUtil.currentSession(); <programlisting><![CDATA[Session session = HibernateUtil.getCurrentSession();
Transaction tx = session.beginTransaction(); Transaction tx = session.beginTransaction();
Cat princess = new Cat(); Cat princess = new Cat();
@ -561,7 +561,7 @@ HibernateUtil.closeSession();]]></programlisting>
</para> </para>
<para> <para>
Note that you may call <literal>HibernateUtil.currentSession();</literal> Note that you may call <literal>HibernateUtil.getCurrentSession();</literal>
as many times as you like, you will always get the current <literal>Session</literal> as many times as you like, you will always get the current <literal>Session</literal>
of this thread. You have to make sure the <literal>Session</literal> is closed of this thread. You have to make sure the <literal>Session</literal> is closed
after your unit-of-work completes, either in your servlet code or in a servlet filter after your unit-of-work completes, either in your servlet code or in a servlet filter

View File

@ -56,7 +56,7 @@
database transaction boundaries? database transaction boundaries?
</para> </para>
<sect2 id="transactions-basics-uow"> <sect2 id="transactions-basics-uow" revision="1">
<title>Unit of work</title> <title>Unit of work</title>
<para> <para>
@ -92,10 +92,9 @@
easy access (like accessing a static variable) in all code that runs in this thread. Depending easy access (like accessing a static variable) in all code that runs in this thread. Depending
on the database transaction demarcation mechanism you chose, you might also keep the transaction on the database transaction demarcation mechanism you chose, you might also keep the transaction
context in a <literal>ThreadLocal</literal> variable. The implementation patterns for this context in a <literal>ThreadLocal</literal> variable. The implementation patterns for this
are known as <emphasis>ThreadLocal Session</emphasis> and <emphasis>Open Session in View</emphasis>. are known as <emphasis>ThreadLocal Session</emphasis> and <emphasis>Open Session in View</emphasis>
You can easily extend the <literal>HibernateUtil</literal> helper class shown earlier in this and can be found on the Hibernate Wiki. Of course, you'd have to find a way to implement an
documentation to implement this. Of course, you'd have to find a way to implement an interceptor interceptor and set it up in your environment. See the Hibernate website for tips and examples.
and set it up in your environment. See the Hibernate website for tips and examples.
</para> </para>
</sect2> </sect2>

View File

@ -478,7 +478,7 @@ Total time: 1 second ]]></programlisting>
</sect2> </sect2>
<sect2 id="tutorial-firstapp-helpers"> <sect2 id="tutorial-firstapp-helpers" revision="1">
<title>Startup and helpers</title> <title>Startup and helpers</title>
<para> <para>
@ -518,7 +518,7 @@ public class HibernateUtil {
public static final ThreadLocal session = new ThreadLocal(); public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException { public static Session getCurrentSession() throws HibernateException {
Session s = (Session) session.get(); Session s = (Session) session.get();
// Open a new Session, if this thread has none yet // Open a new Session, if this thread has none yet
if (s == null) { if (s == null) {
@ -542,7 +542,7 @@ public class HibernateUtil {
its static initializer (called once by the JVM when the class is loaded), but also its static initializer (called once by the JVM when the class is loaded), but also
has a <literal>ThreadLocal</literal> variable to hold the has a <literal>ThreadLocal</literal> variable to hold the
<literal>Session</literal> for the current thread. No matter when you call <literal>Session</literal> for the current thread. No matter when you call
<literal>HibernateUtil.currentSession()</literal>, it will always return the same <literal>HibernateUtil.getCurrentSession()</literal>, it will always return the same
Hibernate unit of work in the same thread. A call to <literal>HibernateUtil.closeSession()</literal> Hibernate unit of work in the same thread. A call to <literal>HibernateUtil.closeSession()</literal>
ends the unit of work currently associated with the thread. ends the unit of work currently associated with the thread.
</para> </para>
@ -592,7 +592,7 @@ build.xml]]></programlisting>
</sect2> </sect2>
<sect2 id="tutorial-firstapp-workingpersistence" revision="1"> <sect2 id="tutorial-firstapp-workingpersistence" revision="2">
<title>Loading and storing objects</title> <title>Loading and storing objects</title>
<para> <para>
@ -625,7 +625,7 @@ public class EventManager {
</para> </para>
<programlisting><![CDATA[private void createAndStoreEvent(String title, Date theDate) { <programlisting><![CDATA[private void createAndStoreEvent(String title, Date theDate) {
Session session = HibernateUtil.currentSession(); Session session = HibernateUtil.getCurrentSession();
Transaction tx = session.beginTransaction(); Transaction tx = session.beginTransaction();
Event theEvent = new Event(); Event theEvent = new Event();
@ -722,7 +722,7 @@ else if (args[0].equals("list")) {
</para> </para>
<programlisting><![CDATA[private List listEvents() { <programlisting><![CDATA[private List listEvents() {
Session session = HibernateUtil.currentSession(); Session session = HibernateUtil.getCurrentSession();
Transaction tx = session.beginTransaction(); Transaction tx = session.beginTransaction();
List result = session.createQuery("from Event").list(); List result = session.createQuery("from Event").list();
@ -926,7 +926,7 @@ else if (args[0].equals("list")) {
</sect2> </sect2>
<sect2 id="tutorial-associations-working"> <sect2 id="tutorial-associations-working" revision="1">
<title>Working the association</title> <title>Working the association</title>
<para> <para>
@ -934,7 +934,7 @@ else if (args[0].equals("list")) {
</para> </para>
<programlisting><![CDATA[private void addPersonToEvent(Long personId, Long eventId) { <programlisting><![CDATA[private void addPersonToEvent(Long personId, Long eventId) {
Session session = HibernateUtil.currentSession(); Session session = HibernateUtil.getCurrentSession();
Transaction tx = session.beginTransaction(); Transaction tx = session.beginTransaction();
Person aPerson = (Person) session.load(Person.class, personId); Person aPerson = (Person) session.load(Person.class, personId);
@ -968,7 +968,7 @@ else if (args[0].equals("list")) {
<programlisting><![CDATA[ private void addPersonToEvent(Long personId, Long eventId) { <programlisting><![CDATA[ private void addPersonToEvent(Long personId, Long eventId) {
Session session = HibernateUtil.currentSession(); Session session = HibernateUtil.getCurrentSession();
Transaction tx = session.beginTransaction(); Transaction tx = session.beginTransaction();
Person aPerson = (Person) session.load(Person.class, personId); Person aPerson = (Person) session.load(Person.class, personId);
@ -979,7 +979,7 @@ else if (args[0].equals("list")) {
aPerson.getEvents().add(anEvent); // aPerson is detached aPerson.getEvents().add(anEvent); // aPerson is detached
Session session2 = HibernateUtil.currentSession(); Session session2 = HibernateUtil.getCurrentSession();
Transaction tx2 = session.beginTransaction(); Transaction tx2 = session.beginTransaction();
session2.update(aPerson); // Reattachment of aPerson session2.update(aPerson); // Reattachment of aPerson