You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Rich Garabedian <ri...@autorevenue.com> on 2003/12/19 15:35:59 UTC

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: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: struts, hibernate, datasources .... so lost

Posted by David Erickson <de...@cmcflex.com>.
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.JDBCTransactio
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" <ri...@autorevenue.com>
To: <st...@jakarta.apache.org>
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: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org