Brian,

How do I put this... You don't go crazy with Hibernate until you're
comfortable with it.  Feel free to start with implementing a few actions
which do something like this to obtain a Session from within your action:

// assuming you used the hibernate example listener at:
// http://www.hibernate.org/133.html
// This is from memory so I might be slightly off on
// the method calls since I'm using DAO objects to hide
// the implementation from myself
// DAO examples were posted today such as:
// http://daoexamples.sourceforge.net/xref/index.html
//
Session sess = HibernateListener.sessionFactory(request);
try {
        // open your transaction
        Transaction tx = sess.beginTransaction();
        // Perform your questy
        // etc.
} catch ( HibernateException he ){
        sess.rollback(); // or whatever
} finally {
        // etc.
}
// and so forth
****

Once you are comfortable with that, THEN you should look into more
complicated solutions such as:

A) Using a threalocal
OR
B) Having a Hibernate Session object get created and remove automatically
when the Server creates a session to store objects for you user.  The
methods in that listener class which you would have to implement are
sessionCreated(..) and sessionDestroyed(..).

I don't use ThreadLocal and I get along fine.  I guess that comes from me
being a self-taught hack who really doesn't have a handle on the ThreadLocal
practice. :)

Remember, you should take your time to get used to it before you jump into
the added, but useful, complications of caches, proxying, ThreadLocals, etc.

Regards,
David

-----Original Message-----
From: Brian McGovern [mailto:[EMAIL PROTECTED]
Sent: Wednesday, March 30, 2005 8:53 AM
To: Struts Users Mailing List
Subject: RE: Struts , hibernate, and DBCP


David , OK I got this working.  It creates the pool on startup.  But the
question now becomes how to get a session from the factory.

This listener provides a open(HttpServletRequest) method, but no close
method.  I intend to wrap my data access objects in a separate layer and
would like to avoid passing the current HttpServletRequest to the data
layer.  I'm probably just missing something obvious here, sorry.

Hibernate recommends implementing a utility class that uses ThreadLocal and
provides open and close methods for the current session.  I've created
called this object's currentSession  method from a startup Servlet which
gets my pool to be created on app server startup.  I'm guessing this is a
sloppier approach than implementing the Listener right?

Thanks
-B



-----Original Message-----
From: David G. Friedman [mailto:[EMAIL PROTECTED]
Sent: Tuesday, March 29, 2005 6:37 PM
To: Struts Users Mailing List
Subject: RE: Struts , hibernate, and DBCP


Brian,

If you use a Tomcat resource reference to initialize your DB pool, be
careful where you put it: the location you place that init reference makes
the difference between YOUR webapp using the hibernate pool and ANY webapp
being able to use your hibernate pool.

Now, since someone posted about a Servlet Context Listener starting up
Hibernate for you, what is wrong with the one given on the hibernate site
under the page title "Tomcat Event listener for Hibernate" at the URL
http://www.hibernate.org/133.html ?

The example code shows how to have the hibernate configuration read.  You
can easily put pool information in your hibernate.cfg.xml file so Hibernate
starts the DB Pool for you.  (I do that)  Additionally, it lists how, should
you need it, to have a DB Pool instance automatically given out when a
session object is created and how to remove it when a session is ended.

Lastly, if you use C3P0, BE VERY CAREFUL if you are using version 0.8.3 of
C3P0 - there is no proper DB Pool shutdown code in Hibernate if the pool is
of that type (I last checked Hibernate 2.1.6's pool shutdown code).
Proxool, EHCache, and various other pool software have code in the db class
shutdown method in as far as I know.  (I had to research this when I had a
memory issue and kept restarting my webapp yet memory usage kept rising and
I kept seeing new DB pools each webapp restart.)

Regards,
David

-----Original Message-----
From: Brian McGovern [mailto:[EMAIL PROTECTED]
Sent: Tuesday, March 29, 2005 1:41 PM
To: Struts Users Mailing List
Subject: Struts , hibernate, and DBCP


Im using struts, hibernate and dbcp connection pooling.  Everything works
fine but regarding my connection pool.  It gets intantiated on the first
time I request a connection from the DBCP pool.  I want it to create the
pool when tomcat starts.  I think i can do this with struts, but im not sure
how.  If using struts config for htis is not the answer i;d still like to
know what is.

-thanks

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to