小编典典

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

tomcat

我在一个项目中工作,我想在tomcat上使用hibernate 配置一个 共享连接 池。该项目已经实施,我必须对其进行更改。它配置有
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-connector-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>

那就是我到目前为止所做的。我的问题是。在我的项目中,我有一个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:

编辑

我没有提到的是,IAM使用OSGI昼夜平分线和Servlet桥。我配置的所有内容都是正确的,在这种情况下,我唯一需要更改的就是更改我的
launch.ini

osgi.parentClassloader=app
osgi.contextClassLoaderParent=app

更改为

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

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


阅读 308

收藏
2020-06-16

共1个答案

小编典典

osgi.parentClassloader=app
osgi.contextClassLoaderParent=app

更改为

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

是我遇到
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.
2020-06-16