在tomcat上使用hibernate和mysql共享c3p0连接池

时间:2017-06-28 11:52:02

标签: java hibernate tomcat connection-pooling c3p0

我正在开发一个项目,我希望在tomcat上使用hibernate配置共享连接池。 该项目已经实施,我必须改变它。 它配置了 hibernate和c3p0连接池,其中所有jar都在项目本身。

我将连接池的所有jar文件复制到 tomcat lib 文件夹中。

  1. c3p0-0.9.5.2.jar
  2. C3P0-Oracle的薄额外-0.9.5.2.jar
  3. mchange-公地java的0.2.11.jar
  4. MySQL的连接器的Java-5.1.40-bin.jar
  5. server.xml 中我创建了一个资源

    <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" />  
    

    之后我可以在phpmyadmin(mysql)中看到我的连接创建了。

    在我创建的项目的 web.xml 文件中 资源参考。

      <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>
    `
    

    我的 hibernate.cfg.xml 文件 `    

      </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>
    

    这就是我所做的事情。我的问题是。在我的项目中,我有一个sessionfactory,我在其中设置了我的hibernate.cfg.xml和Database.hbm.xml文件。 现在这给了我一个 java.lang.NullPointerException

    我是否还必须在tomcat上配置sessionfactory作为资源?

    在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:
    

    修改

    我没有提到过,我使用的是OSGI equinox和一个servlet桥。 我配置的所有内容都是正确的,在我的情况下我唯一需要改变的是改变我的 launch.ini  我有:

    osgi.parentClassloader=app
    osgi.contextClassLoaderParent=app
    

    将其更改为

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

    here a link where i got my help from

    也在使用 java:/ comp / env / jdbc / testDB comp 之前使用斜杠解决了我的问题

2 个答案:

答案 0 :(得分:2)

server.xml 中的

更改为:

 <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" />

和:

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

hibernate.cfg.xml 文件

 <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>

重新启动catalina。

答案 1 :(得分:0)

osgi.parentClassloader=app
osgi.contextClassLoaderParent=app

将其更改为

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

here我从 NoInitialContextException 获得帮助的链接。

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

还使用 java:/ comp / env / jdbc / testDB 而不是 java:comp / env / jdbc / testDB (在 / comp )解决了我在linux tomcat服务器上遇到的另一个问题(JNDI查找异常):

NameNotFoundException.