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:
parent
57ba6ab422
commit
0fa02198d8
|
@ -1602,16 +1602,15 @@ hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect]]></programlisting>
|
|||
|
||||
</sect2>
|
||||
|
||||
<sect2 id="configuration-j2ee-currentsession" revision="1">
|
||||
<sect2 id="configuration-j2ee-currentsession" revision="2">
|
||||
<title>Automatic JTA and Session binding</title>
|
||||
|
||||
<para>
|
||||
For non-managed environments we suggested <literal>HibernateUtil</literal> with
|
||||
a static <literal>SessionFactory</literal>, and <literal>ThreadLocal</literal>
|
||||
management of the Hibernate <literal>Session</literal>. This approach isn't easy
|
||||
to use in an EJB environment, as several EJB's may execute inside the same
|
||||
transaction but not the same thread. We recommend that you bind the
|
||||
<literal>SessionFactory</literal> to JNDI in a managend environment.
|
||||
We recommend that you bind the <literal>SessionFactory</literal> to JNDI in
|
||||
a managend environment. For transaction and <literal>Session</literal> handling,
|
||||
you could use the already introduced <literal>HibernateUtil</literal> helper class.
|
||||
However, EJBs might not execute in the same thread, which makes <literal>ThreadLocal</literal>
|
||||
handling not always appropriate (e.g. when two session beans call each other).
|
||||
</para>
|
||||
|
||||
<para>
|
||||
|
@ -1623,7 +1622,11 @@ hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect]]></programlisting>
|
|||
and <literal>hibernate.transaction.auto_close_session</literal> configuration option,
|
||||
will be set automatically for every <literal>Session</literal> you retrieve with
|
||||
<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>
|
||||
|
|
|
@ -442,7 +442,7 @@ Indexes: cat_pkey primary key btree (cat_id)]]></programlisting>
|
|||
|
||||
</sect1>
|
||||
|
||||
<sect1 id="quickstart-playingwithcats" revision="2">
|
||||
<sect1 id="quickstart-playingwithcats" revision="3">
|
||||
<title>Playing with cats</title>
|
||||
|
||||
<para>
|
||||
|
@ -503,7 +503,7 @@ public class HibernateUtil {
|
|||
|
||||
public static final ThreadLocal session = new ThreadLocal();
|
||||
|
||||
public static Session currentSession() {
|
||||
public static Session getCurrentSession() {
|
||||
Session s = (Session) session.get();
|
||||
// Open a new Session, if this Thread has none yet
|
||||
if (s == null) {
|
||||
|
@ -539,7 +539,7 @@ public class HibernateUtil {
|
|||
<literal>process()</literal> method might look like this (sans exception handling):
|
||||
</para>
|
||||
|
||||
<programlisting><![CDATA[Session session = HibernateUtil.currentSession();
|
||||
<programlisting><![CDATA[Session session = HibernateUtil.getCurrentSession();
|
||||
Transaction tx = session.beginTransaction();
|
||||
|
||||
Cat princess = new Cat();
|
||||
|
@ -561,7 +561,7 @@ HibernateUtil.closeSession();]]></programlisting>
|
|||
</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>
|
||||
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
|
||||
|
|
|
@ -56,7 +56,7 @@
|
|||
database transaction boundaries?
|
||||
</para>
|
||||
|
||||
<sect2 id="transactions-basics-uow">
|
||||
<sect2 id="transactions-basics-uow" revision="1">
|
||||
<title>Unit of work</title>
|
||||
|
||||
<para>
|
||||
|
@ -92,10 +92,9 @@
|
|||
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
|
||||
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>.
|
||||
You can easily extend the <literal>HibernateUtil</literal> helper class shown earlier in this
|
||||
documentation to implement this. Of course, you'd have to find a way to implement an interceptor
|
||||
and set it up in your environment. See the Hibernate website for tips and examples.
|
||||
are known as <emphasis>ThreadLocal Session</emphasis> and <emphasis>Open Session in View</emphasis>
|
||||
and can be found on the Hibernate Wiki. Of course, you'd have to find a way to implement an
|
||||
interceptor and set it up in your environment. See the Hibernate website for tips and examples.
|
||||
</para>
|
||||
|
||||
</sect2>
|
||||
|
|
|
@ -478,7 +478,7 @@ Total time: 1 second ]]></programlisting>
|
|||
|
||||
</sect2>
|
||||
|
||||
<sect2 id="tutorial-firstapp-helpers">
|
||||
<sect2 id="tutorial-firstapp-helpers" revision="1">
|
||||
<title>Startup and helpers</title>
|
||||
|
||||
<para>
|
||||
|
@ -518,7 +518,7 @@ public class HibernateUtil {
|
|||
|
||||
public static final ThreadLocal session = new ThreadLocal();
|
||||
|
||||
public static Session currentSession() throws HibernateException {
|
||||
public static Session getCurrentSession() throws HibernateException {
|
||||
Session s = (Session) session.get();
|
||||
// Open a new Session, if this thread has none yet
|
||||
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
|
||||
has a <literal>ThreadLocal</literal> variable to hold the
|
||||
<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>
|
||||
ends the unit of work currently associated with the thread.
|
||||
</para>
|
||||
|
@ -592,7 +592,7 @@ build.xml]]></programlisting>
|
|||
|
||||
</sect2>
|
||||
|
||||
<sect2 id="tutorial-firstapp-workingpersistence" revision="1">
|
||||
<sect2 id="tutorial-firstapp-workingpersistence" revision="2">
|
||||
<title>Loading and storing objects</title>
|
||||
|
||||
<para>
|
||||
|
@ -625,7 +625,7 @@ public class EventManager {
|
|||
</para>
|
||||
|
||||
<programlisting><![CDATA[private void createAndStoreEvent(String title, Date theDate) {
|
||||
Session session = HibernateUtil.currentSession();
|
||||
Session session = HibernateUtil.getCurrentSession();
|
||||
Transaction tx = session.beginTransaction();
|
||||
|
||||
Event theEvent = new Event();
|
||||
|
@ -722,7 +722,7 @@ else if (args[0].equals("list")) {
|
|||
</para>
|
||||
|
||||
<programlisting><![CDATA[private List listEvents() {
|
||||
Session session = HibernateUtil.currentSession();
|
||||
Session session = HibernateUtil.getCurrentSession();
|
||||
Transaction tx = session.beginTransaction();
|
||||
|
||||
List result = session.createQuery("from Event").list();
|
||||
|
@ -926,7 +926,7 @@ else if (args[0].equals("list")) {
|
|||
|
||||
</sect2>
|
||||
|
||||
<sect2 id="tutorial-associations-working">
|
||||
<sect2 id="tutorial-associations-working" revision="1">
|
||||
<title>Working the association</title>
|
||||
|
||||
<para>
|
||||
|
@ -934,7 +934,7 @@ else if (args[0].equals("list")) {
|
|||
</para>
|
||||
|
||||
<programlisting><![CDATA[private void addPersonToEvent(Long personId, Long eventId) {
|
||||
Session session = HibernateUtil.currentSession();
|
||||
Session session = HibernateUtil.getCurrentSession();
|
||||
Transaction tx = session.beginTransaction();
|
||||
|
||||
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) {
|
||||
|
||||
Session session = HibernateUtil.currentSession();
|
||||
Session session = HibernateUtil.getCurrentSession();
|
||||
Transaction tx = session.beginTransaction();
|
||||
|
||||
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
|
||||
|
||||
Session session2 = HibernateUtil.currentSession();
|
||||
Session session2 = HibernateUtil.getCurrentSession();
|
||||
Transaction tx2 = session.beginTransaction();
|
||||
|
||||
session2.update(aPerson); // Reattachment of aPerson
|
||||
|
|
Loading…
Reference in New Issue