0
votes

Iam working on a project where i want to configure a shared connection pool with hibernate on tomcat. The project is already implemented and i have to change it. It was configured with hibernate and c3p0 connection pool where all jars where in the project itself.

I copied all jar files for connection pooling into the lib folder of tomcat.

  1. c3p0-0.9.5.2.jar
  2. c3p0-oracle-thin-extras-0.9.5.2.jar
  3. mchange-commons-java-0.2.11.jar
  4. mysql-connector-java-5.1.40-bin.jar

in server.xml i made a Resource

<Resource auth="Container"
      description="DB Connection"
      driverClass="com.mysql.jdbc.Driver"
      maxPoolSize="40"
      minPoolSize="10"
      acquireIncrement="1"
      name="jdbc/test"
      user="admin"
      password="root"
      factory="org.apache.naming.factory.BeanFactory"
      type="com.mchange.v2.c3p0.ComboPooledDataSource"
      jdbcUrl="jdbc:mysql://localhost:3306/testDB" />
      

       <ResourceLink
 global="jdbc/test"
 name="jdbc/test"
 type="javax.sql.DataSource" />  

after this i can see my connections created by the pool in phpmyadmin(mysql).

In my web.xml file of my project i created a resource-ref.`

  <resource-ref>
  <description>DB Connection</description>
  <res-ref-name>jdbc/testDB</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
 </resource-ref>
`

And my hibernate.cfg.xml file `

  </hibernate-configuration>
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="connection.datasource">java:comp/env/jdbc/testDB</property>
  <mapping resource="Database.hbm.xml"/>
  </hibernate-configuration>

Thats what i have done till yet. My problem is. In my project i have a sessionfactory where i set my hibernate.cfg.xml and Database.hbm.xml file. This is now giving me a java.lang.NullPointerException.

Do i have to configure the sessionfactory also on tomcat as resource?

At Tomcat:

 Jun 29, 2017 10:06:19 AM org.apache.catalina.mbeans.GlobalResourcesLifecycleListener createMBeans
SCHWERWIEGEND: Exception processing Global JNDI Resources
javax.naming.NamingException: Could not load resource factory class [Root exception is java.lang.ClassNotFoundException: myutil.hibernate.HibernateSessionFactoryTomcatFactory]
    at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:82)
    at javax.naming.spi.NamingManager.getObjectInstance(Unknown Source)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:842)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
    at org.apache.naming.NamingContextBindingsEnumeration.nextElementInternal(NamingContextBindingsEnumeration.java:117)
    at org.apache.naming.NamingContextBindingsEnumeration.next(NamingContextBindingsEnumeration.java:71)
    at org.apache.naming.NamingContextBindingsEnumeration.next(NamingContextBindingsEnumeration.java:34)
    at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:138)
    at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:145)
    at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:110)
    at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.lifecycleEvent(GlobalResourcesLifecycleListener.java:82)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:402)
    at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:347)
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:732)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:689)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:321)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:455)
Caused by: java.lang.ClassNotFoundException: myutil.hibernate.HibernateSessionFactoryTomcatFactory
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:80)
    ... 23 more

 
Jun 29, 2017 10:06:17 AM org.apache.tomcat.util.digester.Digester endElement
WARNUNG:   No rules found matching 'Server/GlobalNamingResources/ResourceLink'.
Jun 29, 2017 10:06:17 AM org.apache.catalina.core.AprLifecycleListener init
INFORMATION: The APR based Apache Tomcat Native library which allows optimal
 performance in production environments was not found on the java.library.path:

EDIT

What i have not mentioned, iam using OSGI equinox and a servlet bridge. everything that i have configured is right, the only thing i had to change in my case was to change my launch.ini i had:

osgi.parentClassloader=app
osgi.contextClassLoaderParent=app

changing it to

osgi.parentClassloader=app
osgi.contextClassLoaderParent=fwk <--- changed

here a link where i got my help from

also using java:/comp/env/jdbc/testDB using a slash before comp solved my problem

2

2 Answers

2
votes

in your server.xml Change to this :

 <Resource auth="Container"
  description="DB Connection"
  driverClass="com.mysql.jdbc.Driver"
  maxPoolSize="40"
  minPoolSize="10"
  acquireIncrement="1"
  name="jdbc/testDB" <!-- change -->
  user="admin"
  password="root"
  factory="org.apache.naming.factory.BeanFactory"
  type="com.mchange.v2.c3p0.ComboPooledDataSource"
  jdbcUrl="jdbc:mysql://localhost:3306/testDB" />

and :

 <ResourceLink
  global="jdbc/testDB" <!--change -->
  name="jdbc/testDB"   <!-- & change -->
  type="javax.sql.DataSource" />  

And your hibernate.cfg.xml file to

 <hibernate-configuration>
  <session-factory>
   <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
   <property name="connection.datasource">java:comp/env/jdbc/testDB</property> <!-- ? -->
  <mapping resource="Database.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

restart catalina.

0
votes
osgi.parentClassloader=app
osgi.contextClassLoaderParent=app

changing it to

osgi.parentClassloader=app
osgi.contextClassLoaderParent=fwk <--- changed

here a link where i got my help from when i had NoInitialContextException.

javax.naming.NoInitialContextException:Cannot instantiate class: org.apache.naming.java.javaURLContextFactory (root cause classnotfound for org.apache.naming.java.javaURLContextFactory)

also using java:/comp/env/jdbc/testDB instead of java:comp/env/jdbc/testDB (slash before /comp) solved a different problem i had on the linux tomcat server(JNDI lookup exception):

NameNotFoundException.