Try not specifying the "factory" parameter. It isn't required anyway by Tomcat-5.5.x. Tomcat-5.5.x uses its own version of commons-dbcp, so if you are specifying "org.apache.commons.dbcp.BasicDataSourceFactory", that is probably not the one that Tomcat wants to use internally. I think they wrapped up the package into their own, such as maybe "org.apache.tomcat.commons.dbcp.BasicDataSourceFactory" (this may not be exact, I just know it is something like this), to avoid version collisions with user-provided commons-dbcp jars.


Jake

At 09:42 PM 2/5/2005 +0100, you wrote:
>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.ja
>va:540)
> at
>net.sf.hibernate.connection.DatasourceConnectionProvider.getConnection(Datas
>ourceConnectionProvider.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]



Reply via email to