简体   繁体   English

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

[英]shared c3p0 connection pool with hibernate and mysql on tomcat

Iam working on a project where i want to configure a shared connection pool with hibernate on tomcat. 我在一个项目中工作,我想在tomcat上使用hibernate配置一个共享连接池。 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. 它配置有hibernate和c3p0连接池 ,其中所有jar都位于项目本身中。

I copied all jar files for connection pooling into the lib folder of tomcat . 我将所有用于连接池的jar文件复制到了tomcatlib文件夹中。

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

in server.xml i made a Resource 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" />  

after this i can see my connections created by the pool in phpmyadmin(mysql). 之后,我可以在phpmyadmin(mysql)中看到由池创建的连接。

In my web.xml file of my project i created a resource-ref.` 在我的项目的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>
`

And my hibernate.cfg.xml file ` 还有我的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>

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. 在我的项目中,我有一个sessionfactory,其中设置了hibernate.cfg.xml和Database.hbm.xml文件。 This is now giving me a java.lang.NullPointerException . 现在,这给了我一个java.lang.NullPointerException

Do i have to configure the sessionfactory also on tomcat as resource? 我是否也必须在tomcat上将sessionfactory配置为资源?

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

EDIT 编辑

What i have not mentioned, iam using OSGI equinox and a servlet bridge. 我没有提到的是,IAM使用OSGI昼夜平分线和Servlet桥。 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: 我配置的所有内容都是正确的,在这种情况下,我唯一需要更改的就是更改我的launch.ini

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 comp解决我的问题之前使用斜杠使用java:/ comp / env / jdbc / testDB

in your server.xml Change to this : 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" />

and : 和:

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

And your hibernate.cfg.xml file to 然后将您的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>

restart catalina. 重新启动卡塔利娜。

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 . 是我遇到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): 还使用java:/ comp / env / jdbc / testDB代替java:comp / env / jdbc / testDB/ comp之前的斜杠)解决了我在linux tomcat服务器上遇到的另一个问题(JNDI查找异常):

NameNotFoundException.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM