在 tomcat 上与休眠和 mysql 共享 c3p0 连接池

shared c3p0 connection pool with hibernate and mysql on tomcat

我正在做一个项目,我想在 tomcat 上配置一个 共享连接 休眠池。 该项目已经实施,我必须更改它。 它配置了 hibernate 和 c3p0 连接池,其中所有 jar 都在项目本身中。

我将连接池的所有 jar 文件复制到 tomcat.

lib 文件夹中
  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-连接器-java-5.1.40-bin.jar

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>

这就是我到目前为止所做的。我的问题是。在我的项目中,我有一个会话工厂,我在其中设置 hibernate.cfg.xml 和 Database.hbm.xml 文件。 这现在给了我一个 java.lang.NullPointerException.

我是否还必须将 tomcat 上的会话工厂配置为资源?

在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/testDBcomp 之前使用斜杠解决了我的问题

在你的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>

重启卡特琳娜。

osgi.parentClassloader=app
osgi.contextClassLoaderParent=app

改为

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

here a link 当我遇到 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.