That code should work assuming your object is fully loaded by the time it
gets to the view and no longer needs the session.  Also I would use
Transactions in your code even on simple query's.. that was the advice given
on hibernate's site.  ie: Transaction tx = session.beginTransaction().  then
tx.commit() when done or rollback or whatever.

Also I don't know what you are using as your log manager but using log4j
when I get a hibernate error I also send it the exception and it prints a
stack trace for me which gives more information on what went wrong.

Can't say that I've seen that behavior with tomcat myself =).. I have a
whole other can of worms I've had to deal with involving that...

-David

----- Original Message ----- 
From: "Rich Garabedian" <[EMAIL PROTECTED]>
To: "'Struts Users Mailing List'" <[EMAIL PROTECTED]>
Sent: Friday, December 19, 2003 2:10 PM
Subject: RE: struts, hibernate, datasources .... so lost


> I think that makes sense. So basically you are saying I HAVE to
> implement the filter and doing something like this:
>
> try
> {
>     Context ctx = new InitialContext();
>     SessionFactory sf =
> (SessionFactory)ctx.lookup("prospecting/hibernate/SessionFactory");
>     Session session = sf.openSession();
>
>     Query query = session.createQuery("select e.pwd from
> com.autorevenue.prospecting.hibernate.Employee e where e.email =
> :email");
>     query.setString("email", email);
>
>     e = (Employee)query.uniqueResult();
>
>     if( e != null)
>     {
>         password = e.getPwd();
>     }
>
>     session.close();
> }
> catch(Exception exc)
> {
>     servlet.log("Hibernate error " + exc.getMessage());
> }
>
>
> Won't work??
>
> I've made some progress, I think. Log output shows Hibernate is
> returning a value for a given query .... but the code above keeps
> throwing an exception that is simply "Hibernate error null" Perhaps I'm
> getting that because I'm not using the filter yet?
>
> Another frustrating thing is that my container based auth stopped
> working. Keep getting "Name jdbc is not bound in this Context". Have to
> play with that some more.
>
> Another weird thing. Copying a new war to the war directory and having
> Tomcat reload my war doesn't seem to work anymore. I mean, it does load,
> but when it does changes to the Hibernate config aren't updated. Only
> way, so far, to see changes has been to shutdown server, deleted war
> filed and war directory, copy new war file to webapp directory, and
> restart. Does the same thing happen to anyone else?
>
> > -----Original Message-----
> > From: David Erickson [mailto:[EMAIL PROTECTED]
> > Sent: Friday, December 19, 2003 1:31 PM
> > To: [EMAIL PROTECTED]
> > Subject: Re: struts, hibernate, datasources .... so lost
> >
> > Rich,
> > Ya the binding thing with JNDI and tomcat is messed up.. for some
> reason
> > I've read its Read Only.. but the strange thing is I WAS able to get
> it
> > working when I bound to that resource from a static class, but as soon
> as
> > I
> > used a filter or a struts plugin it no longer worked.. really lame.
> >
> > Regarding what happens after you load something from Hibernate.  I'm
> still
> > learning this myself, but I assume if you don't load ANYTHING lazily
> then
> > you should be able to do anything with that variable and have no
> worries.
> > However if you have an object that contains say a set of other
> objects, ie
> > a
> > team with a set of players, and set it to lazy loading, meaning when
> you
> > load team 1, the players arent loaded until you call, getPlayers(),
> then
> > at
> > that point hibernate is still bound to your variable and will load
> that
> > from
> > the db assuming the session you initially loaded the team with is
> still
> > open.  Make sense?  The way it works is hibernate has its own classes
> > inherited from set/list/map etc that handle that.. and it uses those
> when
> > you load sets.
> > -David
> >
> > ----- Original Message -----
> > From: "Rich Garabedian" <[EMAIL PROTECTED]>
> > To: "'David Erickson'" <[EMAIL PROTECTED]>
> > Sent: Friday, December 19, 2003 11:12 AM
> > Subject: RE: struts, hibernate, datasources .... so lost
> >
> >
> > > David,
> > >
> > > David, thanks so much for your note.
> > >
> > > I guess I'll try having Hibernate setup the datasource if I can't
> get it
> > > to work they way I'm trying now. Doing that really bothers me
> though. I
> > > thought datasources were supposed to allow us to configure a single
> > > point of entry and then just use the logical name everywhere ... and
> not
> > > to have to worry about anything else. Need to change your database?
> Just
> > > look in one location and change - easy as pie. Yeah, right.
> Configuring
> > > a JDBC realm for authentication, and then a datasource in an
> entirely
> > > different file just seems plain backwards to me. Grrrh.
> > >
> > > Sorry, I'm really frustrated right now. I spent hours trying to
> > > determine why my web app was crashing on load. Come to find out,
> > > ehcache.jar is required (at least it seems to be for me). Even
> though
> > > the hibernate lib/readme says it's optional. (I can't find anywhere
> > > where I specify cache useage).
> > >
> > >
> > > The explanation below helps clear things up, but there still is one
> > > thing I really don't understand. If, in my action, I use hibernate
> to
> > > grab something from the data layer and store it in a local variable
> ...
> > > don't I know hold that data in my hands. If the session closes now,
> then
> > > who cares? I've already copied it into a local variable and can do
> what
> > > I please with it. Right?
> > >
> > > Dunno, I'm sure that last question readily shows my ignorance of how
> the
> > > entire processes works :-)
> > >
> > > > -----Original Message-----
> > > > From: David Erickson [mailto:[EMAIL PROTECTED]
> > > > Sent: Friday, December 19, 2003 1:00 PM
> > > > To: Struts Users Mailing List; [EMAIL PROTECTED]
> > > > Subject: Re: struts, hibernate, datasources .... so lost
> > > >
> > > > Rich I know how you feel.  I just dealt with nearly the same
> problems
> > > the
> > > > other day.  I for the life of me could not get Hibernate to bind
> to my
> > > > JNDI
> > > > datasource.. so I bailed on that and in my hibernate.cfg.xml I
> just
> > > had it
> > > > setup the datasource and manage it from there:
> > > >
> > > > <hibernate-configuration>
> > > >
> > > > <session-factory name="salesweb:/hibernate/SessionFactory">
> > > >
> > > > <property
> > > name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
> > > >
> > > > <property name="show_sql">true</property>
> > > >
> > > > <property name="connection.username">user</property>
> > > >
> > > > <property name="connection.password">pass</property>
> > > >
> > > > <property
> > > >
> > >
> name="connection.url">jdbc:mysql://192.168.0.104:3306/salesweb</property
> > > >
> > > >
> > > > <property
> > > name="connection.driver_class">com.mysql.jdbc.Driver</property>
> > > >
> > > > <property name="use_outer_join">true</property>
> > > >
> > > > <property
> > > >
> > >
> name="transaction.factory_class">net.sf.hibernate.transaction.JDBCTransa
> > > ct
> > > > io
> > > > nFactory</property>
> > > >
> > > > <property name="dbcp.minIdle">1</property>
> > > >
> > > > </session-factory>
> > > >
> > > > </hibernate-configuration>
> > > >
> > > > etc.  So what hibernate does when you build its config is load up
> a
> > > JNDI
> > > > name and bind its session to that at
> > > salesweb:/hibernate/SessionFactory.
> > > > Then I am using the hibernateFilter which the first run through
> will
> > > build
> > > > that config and also store a static instance of the factory within
> > > that
> > > > class.  The reason its important to use the filter (to my
> > > understanding)
> > > > is
> > > > this:
> > > >
> > > > User requests a webpage, goes to struts action, action calls
> > > getSession()
> > > > from the hibernatefilter, it binds that session to the current
> > > executing
> > > > thread and ships it to the action.  Action loads the requested
> items,
> > > > perhaps the item has a collection that is loaded lazily, so it
> doesnt
> > > > actually get loaded in the action.  Action finishes and forwards
> to
> > > view
> > > > (Note this is in the same thread!!), the view is a jsp that wants
> to
> > > > render
> > > > that lazily loaded collection.  Well if you closed the session in
> your
> > > > action this would fail.  but because the session is still
> maintained
> > > until
> > > > the request is fully finished it succeeds.  Make sense?  Anyway
> thats
> > > my
> > > > understanding, I could be flawed =)
> > > >
> > > > Good luck!
> > > > -David
> > > >
> > > > ----- Original Message -----
> > > > From: "Rich Garabedian" <[EMAIL PROTECTED]>
> > > > To: <[EMAIL PROTECTED]>
> > > > Sent: Friday, December 19, 2003 7:35 AM
> > > > Subject: struts, hibernate, datasources .... so lost
> > > >
> > > >
> > > > > After spending days on the net and the mailing lists I find
> > > something is
> > > > > just not getting through to me. I feel that what I'm doing is
> > > > > conceptually simple; yet I can't seem to actually implement any
> of
> > > it. I
> > > > > think I have a rudimentary understanding of how to integrate
> > > Hibernate
> > > > > into Struts, but I'm unsure on a few points.
> > > > >
> > > > > First the datasource. I'm trying to set up a default data source
> > > that
> > > > > all web applications can use. I use container based
> authentication
> > > and I
> > > > > want my realm to use it as well. Trolling these lists showed me
> a
> > > > > solution that does work with my realm (XML fragments follow
> below).
> > > But
> > > > > when I try to use that same datasource with Hibernate, my web
> > > > > application fails to load and I get the following error:
> > > > >
> > > > > SEVERE: Could not find datasource:
> java:comp/env/jdbc/prospectingDB
> > > > > javax.naming.NameNotFoundException: Name jdbc is not bound in
> this
> > > > > Context
> > > > >
> > > > > Here are the XML fragments from server.xml.
> > > > >
> > > > > First, I set up a global resource:
> > > > >
> > > > > <GlobalNamingResources>
> > > > >
> > > > > <Resource name  = "jdbc/prospectingDB"
> > > > >           auth  = "Container"
> > > > >           scope = "Shareable"
> > > > >           type  = "javax.sql.DataSource"/>
> > > > >
> > > > > <ResourceParams name="jdbc/prospectingDB">
> > > > >
> > > > > ... Usual parameters go here
> > > > >
> > > > > Right under the engine tag I define my realm. This works for my
> > > > > container based auth
> > > > >
> > > > > <Engine name="Catalina" defaultHost="localhost" debug="10">
> > > > >
> > > > > <Realm className      =
> "org.apache.catalina.realm.DataSourceRealm"
> > > > > debug          = "99"
> > > > >        dataSourceName = "jdbc/prospectingDB"
> > > > >        userTable      = "auth_roles_view"
> > > > >        userNameCol    = "email"
> > > > >        userCredCol    = "pwd"
> > > > >        userRoleTable  = "auth_roles_view"
> > > > >        roleNameCol    = "role"/>
> > > > >
> > > > >
> > > > > Then I set up a resource link in the default context:
> > > > >
> > > > > <DefaultContext>
> > > > > <ResourceLink name   = "jdbc/prospectingDB"
> > > > >               global = "jdbc/prospectingDB"
> > > > >               type   = "javax.sql.DataSource" />
> > > > > </DefaultContext>
> > > > >
> > > > > As I said, that all seems to work. But Hibernate bails:
> > > > >
> > > > > <hibernate-configuration>
> > > > >   <session-factory name="prospecting/hibernate/SessionFactory">
> > > > >   <property
> > > > >
> > >
> name="connection.datasource">java:comp/env/jdbc/prospectingDB</property>
> > > > >   <property name="show_sql">false</property>
> > > > >   <property
> > > > >
> name="dialect">net.sf.hibernate.dialect.PostgreSQLDialect</property>
> > > > >   <mapping resource="Employee.hbm.xml"/>
> > > > >   </session-factory>
> > > > > </hibernate-configuration>
> > > > >
> > > > > I can't seem to figure out why. If I change the server.xml to
> define
> > > the
> > > > > datasource in my web-app context, then I get problems with
> container
> > > > > based authentication not finding the datasource??
> > > > >
> > > > > ... and speaking of Hibernate, I think I figured out how to use
> the
> > > > > HibernatePlugIn. I use that to set-up a Hibernate session
> factory on
> > > web
> > > > > app instantiation. But what I'm not 100% sure on is why I need
> to
> > > set-up
> > > > > a filter to implement the open session in view pattern. One of
> the
> > > > > examples I found on the hibernate site doesn't seem to use that
> > > filter.
> > > > > Instead, it looks up the session factory via JNDI right in the
> > > action
> > > > > class. Do I have this right? Could I use either method to grab a
> > > > > Hibernate session? If I extend the action class to do the JNDI
> > > look-up
> > > > > (so I only need to write the lookup code once), would either
> method
> > > > > provide a benefit over the other?
> > > > >
> > > > > I'm at a point where I feel like I have enough knowledge to
> build an
> > > > > app, but at the same time I don't really understand the
> technology.
> > > > > That's kind of precarious - I'm afraid I'm going to leave
> sessions
> > > open
> > > > > ... or something else that will cause errors and be hard to
> track
> > > down.
> > > > >
> > > > > Thank in advance for any advice!
> > > > >
> > > > >
> > > > >
> > > > >
> > >
> ---------------------------------------------------------------------
> > > > > 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