The easiest way to add a JNDI datasource is to install the webadmin
and define the datasource not as a global JNDI but 'local' to the
context. This can easily be achieved with the admin application.

I have seen this problem where I defined the global datasource in TC
5.5 and it was not visible via a JNDI lookup. Either you have to do a
lot more configuration or it is a bug. I haven't gotten arround to
investigate this further :(

Wouter

On Sat, 05 Feb 2005 22:17:54 +0100, Mario Winterer
<[EMAIL PROTECTED]> wrote:
> Hi Jeroen!
> 
> Two weeks ago I (almost) exactly did what you want to do - make
> hibernate use a JNDI-DataSource defined as a GlobalNamingResource! The
> differences: My database is Oracle and I didn't configure hibernate
> directly because I use the spring-framework in between.
> I experienced problems similar than yours, but after cleaning out some
> "old" stuff it worked! Unfortunately, I do not know, what exactly the
> problem was, but I can give you some hints that may help you!
> 
> Assuming you use Tomcat 5.5, do the following
> 1) Skip the "factory"-attribute in your Resource-definition! It is not
> required because tomcat has a built-in connection pool and automatically
> uses it for JNDI-DataSources.
> 2) Define your JNDI-DataSource in the GlobalNamingContext
> 3) The <resourcelink>-element in meta-inf/context.xml must specify the
> name of the global resource (attribute "global") AND the name, the
> resource should have when linked into the lokal JNDI-context (attribute
> "name"). This (internal) name must match the name specified in your
> hibernate config! I guess that's what you got wrong!
> 
> Your code:
> <ResourceLink name="My Database" global="jdbc/mydb"
> type="javax.sql.Datasource" />
> (You specified "My Database" as internal JNDI-name, but jdbc/mydb in
> hibernate.cfg.xml!!!)
> 
> Better:
> <ResourceLink name="jdbc/mydb" global="jdbc/mydb"
> type="javax.sql.Datasource" />
> (That matches the name you used in your hibernate.cfg.xml:
> "java:env/jdbc/mydb")
> 
> 4) Remove the context-definition file in conf/<Engine>/<host>. At
> deployment time, tomcat copies the context.xml file from the
> meta-inf-directory of your web-application into the
> conf/<Engine>/<host>-directory (and renames it to avoid conflicts).
> 5) The PostgreSQL-driver must be in common/lib. Make sure it is NOT in
> WEB-INF/lib too!
> 
> As I've said in top 3, I think the chief cause of your problem is the
> resourcelink-element!
> (Apart from that: I've seen you wrote your own HibernateSessionFilter. I
> also did that first, but then I decided to use the
> OpenSessionInViewFilter from the spring-framework instead. It works
> great! I'm sure your filter-implementation is not the cause of your
> problems, but maybe the spring-framework is interesting to you!)
> 
> Best regards,
>   Tex
> 
> Jeroen Kransen schrieb:
> 
> > Hello,
> >
> > My problem is so basic that I have no doubt other people have
> > experienced it. Still, I can't find any solutions on the web.
> >
> > I want Tomcat to provide my webapp with DataSources through JNDI. I want
> > Hibernate to use these DataSources. Nothing exciting so far. I
> > configured the BasicDataSourceFactory in the META-INF/context.xml like
> > this:
> >
> >    <Resource name="jdbc/mydb"
> >        auth="Container"
> >        driverClassName="org.postgresql.Driver"
> >        factory="org.apache.commons.dbcp.BasicDataSourceFactory"
> >        username="*****"
> >        password="*****"
> >        type="javax.sql.Datasource"
> >        url="jdbc:postgresql://localhost:5432/mywebapp"
> >        scope="Shareable"
> >        maxActive="10"
> >        maxIdle="100"
> >        maxWait="3000"
> >    />
> >
> > In the web.xml I put:
> >
> >    <filter>
> >        <filter-name>HibernateSessionFilter</filter-name>
> >
> > <filter-class>nl.kransen.mywebapp.context.HibernateSessionFilter</filter-class>
> >
> >    </filter>
> >    <filter-mapping>
> >        <filter-name>HibernateSessionFilter</filter-name>
> >        <url-pattern>/*</url-pattern>
> >    </filter-mapping>
> > ...
> >    <resource-ref>
> >        <description>My database</description>
> >        <res-ref-name>jdbc/mydb</res-ref-name>
> >        <res-type>javax.sql.DataSource</res-type>
> >        <res-auth>Container</res-auth>
> >    </resource-ref>
> >
> > In the hibernate.cfg.xml I make a JNDI reference to the datasource:
> >
> > <hibernate-configuration>
> >    <session-factory>
> >        <property
> > name="connection.datasource">java:comp/env/jdbc/mydb</property>
> >        <property
> > name="dialect">net.sf.hibernate.dialect.PostgreSQLDialect</property>
> >        <property name="show_sql">true</property>
> >
> >        <mapping resource="hibernate-mappings/Aap.hbm.xml" />
> >        <mapping resource="hibernate-mappings/Noot.hbm.xml" />
> >        <mapping resource="hibernate-mappings/Mies.hbm.xml" />
> >    </session-factory>
> > </hibernate-configuration>
> >
> > Now I created a HibernateSessionFilter that will filter any request to
> > the webapp and provide it with a Hibernate Session. In the init() the
> > Hibernate SessionFactory is configured by doing a lookup on JNDI for a
> > DataSource.
> >
> > My problem is that Hibernate can't find the JNDI datasource:
> >
> > 17:44:14,745 INFO  [nl.kransen.mywebapp.context.HibernateSessionFilter]
> > Failed to initialize Hibernate!
> > net.sf.hibernate.HibernateException: Could not find datasource:
> > java:comp/env/jdbc/mydb
> >
> > Instead, I  tried to put the Datasource in the <GlobalNamingResources/>
> > of the server.xml. In the context.xml I put:
> >
> >  <ResourceLink name="My Database"
> >            global="jdbc/mydb"
> >            type="javax.sql.Datasource" />
> >
> > The error I get then is:
> >
> > 20:31:09,550 WARN  [net.sf.hibernate.cfg.SettingsFactory] Could not
> > obtain connection metadata
> > org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC
> > driver of class '' for connect URL 'null'
> >        at
> > org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:780)
> >
> >
> >
> > Instead of initializing the Hibernate SessionFactory in the Filter's
> > init() method, I do it the first time the doFilter() is called. Then I
> > get a similar error:
> >
> > 20:42:35,324 DEBUG [net.sf.hibernate.util.JDBCExceptionReporter] Cannot
> > open connection
> > org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC
> > driver of class '' for connect URL 'null'
> >        at
> > org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:780)
> >
> > ...
> > Caused by: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create
> > JDBC driver of class '' for connect URL 'null'
> >        at
> > org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:780)
> >
> >        at
> > org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
> >
> >        at
> > net.sf.hibernate.connection.DatasourceConnectionProvider.getConnection(DatasourceConnectionProvider.java:59)
> >
> >        at
> > net.sf.hibernate.impl.BatcherImpl.openConnection(BatcherImpl.java:289)
> >        ... 54 more
> > Caused by: java.lang.NullPointerException
> >        at java.util.StringTokenizer.<init>(StringTokenizer.java:182)
> >        at org.postgresql.Driver.parseURL(Driver.java:251)
> >        at org.postgresql.Driver.acceptsURL(Driver.java:159)
> >        at java.sql.DriverManager.getDriver(DriverManager.java:232)
> >        at
> > org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:773)
> >
> >
> >
> > What I really want is the first solution with the JNDI datasource
> > declaration in the webapp context.xml, and preferably initialization of
> > Hibernate in the init() of the Filter. Is it possible that the reason
> > that it doesn't work is that the Filter is first created (and its init()
> > called) BEFORE the DatasourceFactory is created and bound to JNDI? If
> > that is the case, wouldn't it make more sense to turn that around? After
> > all, it's the "CONTEXT.xml" :-)
> >
> > The second and third structures brought me closer, but why does the
> > Postgresql driver think that URL 'null' was passed, when I put a correct
> > URL in the config?
> >
> > I hope anyone can help me.
> >
> > Jeroen Kransen
> >
> >
> > ---------------------------------------------------------------------
> > 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]
> 
> 


-- 
Regards, Wouter Boers
business: http://www.abcdarium.nl
personal: http://www.ikke.net

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

Reply via email to