Openshift Unable to locate persistence units - java

I'm developing Hibernate JPA persistance web application, with my persistence.xml in src/main/resources/META-INF/persistence.xml (in war file it's in WEB-INF/classes/META-INF). It all works well on local Tomcat server, but when put on Openshift JBoss EWS, getting this message on startup:
Caused by: javax.persistence.PersistenceException: Unable to locate persistence units
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:101)
at org.hibernate.ejb.HibernatePersistence.getEntityManagerFactoryBuilderOrNull(HibernatePersistence.java:93)
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:88)
at org.hibernate.ejb.HibernatePersistence.getEntityManagerFactoryBuilderOrNull(HibernatePersistence.java:101)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:69)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at komante.server.SpringConfig.devEntityManagerFactory(SpringConfig.java:29)
at komante.server.SpringConfig$$EnhancerBySpringCGLIB$$17e4a4c6.CGLIB$devEntityManagerFactory$1()
at komante.server.SpringConfig$$EnhancerBySpringCGLIB$$17e4a4c6$$FastClassBySpringCGLIB$$55fcba01.invoke()
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312)
at komante.server.SpringConfig$$EnhancerBySpringCGLIB$$17e4a4c6.devEntityManagerFactory()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
... 28 more
Caused by: java.util.MissingResourceException: Could not load any resource bundle by com.sun.org.apache.xerces.internal.impl.msg.XMLSchemaMessages
at com.sun.org.apache.xerces.internal.utils.SecuritySupport$7.run(SecuritySupport.java:174)
at com.sun.org.apache.xerces.internal.utils.SecuritySupport$7.run(SecuritySupport.java:166)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.org.apache.xerces.internal.utils.SecuritySupport.getResourceBundle(SecuritySupport.java:166)
at com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter.formatMessage(XSMessageFormatter.java:70)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:398)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr(XSDHandler.java:4162)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaError(XSDHandler.java:4145)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDAbstractTraverser.reportSchemaError(XSDAbstractTraverser.java:721)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDSimpleTypeTraverser.getSimpleType(XSDSimpleTypeTraverser.java:406)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDSimpleTypeTraverser.traverseSimpleTypeDecl(XSDSimpleTypeTraverser.java:163)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDSimpleTypeTraverser.traverseGlobal(XSDSimpleTypeTraverser.java:104)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.traverseSchemas(XSDHandler.java:1442)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:630)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:617)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:575)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:541)
at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:252)
at javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:627)
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.resolveLocalSchema(PersistenceXmlParser.java:435)
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.v21Schema(PersistenceXmlParser.java:400)
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.validate(PersistenceXmlParser.java:347)
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.loadUrl(PersistenceXmlParser.java:310)
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.parsePersistenceXml(PersistenceXmlParser.java:114)
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.doResolve(PersistenceXmlParser.java:104)
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.locatePersistenceUnits(PersistenceXmlParser.java:86)
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:97)
Persistence.xml starts with
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
and has two persistence units in it. I alse deleted hibernate.cfg.xml, both things suggested in other SO answers. Anyone know what I'm doing wrong here?

Got it.
The problem was I used Persistence.createEntityManagerFactory("unit") to create EntityManagerFactory and injected it in other Spring beans.
Now I put
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="unit" />
</bean>
directly in applicationContext.xml.
Still doesn't explain what was wrong with previous approach, but it works.

Related

How to solve the problem: java.io.FileNotFoundException when deploying jee app to Wildfly 25

I get the error below when deploying the app to Wildfly:
{"WFLYCTL0080: Failed services" => {"jboss.persistenceunit."funmusic-back.war#productPU"" => "jakarta.persistence.PersistenceException: [PersistenceUnit: productPU] Unable to build Hibernate SessionFactory
Caused by: jakarta.persistence.PersistenceException: [PersistenceUnit: productPU] Unable to build Hibernate SessionFactory
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Unable to open specified script target file for writing : productDrop.ddl
Caused by: java.io.FileNotFoundException: productDrop.ddl (Permission denied)"}}
The tricky point I met is, when I start the wildfly by standalone.sh, it is alright. However, when I start it using systemctl start wildfly, this problem happen. I am wondering whether it is the problem related to the Domain Mode and Standalone Mode of Wildfly, but could not recognize the missing piece I should learn in order to solve the problem. My OS using is CentOS 7 and the Wildfly version is 25.
Below is the persistence.xml for your reference:
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
version="2.2">
<!-- transaction-type is the type of transactions used by EntityManagers from this persistence unit. -->
<persistence-unit name="productPU" transaction-type="JTA">
<properties>
<property name ="javax.persistence.schema-generation.database.action" value="drop-and-create"></property>
<property name ="javax.persistence.schema-generation.scripts.action" value="drop-and-create"></property>
<property name ="javax.persistence.schema-generation.scripts.create-target" value="productCreate.ddl"></property>
<property name ="javax.persistence.schema-generation.scripts.drop-target" value="productDrop.ddl"></property>
</properties>
</persistence-unit>
I get a single #Entity which is a very simple POJO with a few fields within. As it is simple, I am going to skip it here.
It would be grateful if you could point out the cause of the problem and give me some insight of it. Also, document reference with section specified would be more appreciated.
Thank you!

Configure TomEE with Hibernate

I am trying to configure Tomee with Hibernate.
Changes that I made so far :
Copied following jars in tomcat/lib folder :
antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-entitymanager-4.0.1.Final.jar
hibernate-annotations-3.4.0.GA.jar
hibernate-commons-annotations.jar
hibernate-core-3.3.2.GA.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-validator-6.0.9.Final.jar
hibernate-validator-annotation-processor-6.0.9.Final.jar
hibernate-validator-cdi-6.0.9.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
Removed following jars from tomcat/lib :
bval-core-0.5.jar
bval-jsr303-0.5.jar
commons-lang-2.6.jar
openjpa-2.4.0.jar
serp-1.14.1.jar
And persistence.xml looks like this :
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="JPADB" >
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>*****</jta-data-source>
<properties>
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
</properties>
</persistence-unit>
</persistence>
And when I run tomcat, I am getting the following exception :
org.apache.openejb.OpenEJBRuntimeException: java.lang.ClassNotFoundException: org.apache.openjpa.persistence.PersistenceProviderImpl: java.lang.ClassNotFoundException: org.apache.openjpa.persistence.PersistenceProviderImpl
at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:843)
at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:677)
at org.apache.openejb.assembler.classic.Assembler.buildContainerSystem(Assembler.java:568)
at org.apache.openejb.assembler.classic.Assembler.build(Assembler.java:464)
at org.apache.openejb.OpenEJB$Instance.<init>(OpenEJB.java:151)
at org.apache.openejb.OpenEJB.init(OpenEJB.java:309)
at org.apache.tomee.catalina.TomcatLoader.initialize(TomcatLoader.java:256)
at org.apache.tomee.catalina.ServerListener.install(ServerListener.java:167)
at org.apache.tomee.catalina.ServerListener.lifecycleEvent(ServerListener.java:54)
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:394)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:110)
at org.apache.catalina.startup.Catalina.load(Catalina.java:642)
at org.apache.catalina.startup.Catalina.load(Catalina.java:667)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:253)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:427)
Caused by: org.apache.openejb.OpenEJBException: org.apache.openejb.OpenEJBRuntimeException: java.lang.ClassNotFoundException: org.apache.openjpa.persistence.PersistenceProviderImpl: java.lang.ClassNotFoundException: org.apache.openjpa.persistence.PersistenceProviderImpl
at org.apache.openejb.assembler.classic.Assembler.loadPersistenceUnits(Assembler.java:988)
at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:741)
... 20 more
What other changes should I do to make tomee work with hibernate ?

Play 2 framework spring data with MySQL

I am new to Play Framework and I am trying to do a simple example to understand how it works. I have used the template play-spring-data-jpa to start. I would like to use MySQL in a persistent way and that is why I chose it.
I started with h2 to understand how it works and then tried to move to connecting to a MySQL local database.
I followed the steps to use JPA from https://www.playframework.com/documentation/2.1.0/JavaJPA to get a persistence unit.
The code works fine with h2, but when I switch to MySQL, I get an error.
The connection with the database works fine, as I get the message:
[info] play - database [default] connected at jdbc:mysql://localhost/brainUp?useUnicode=yes&characterEncoding=UTF-8&connectionCollation=utf8_general_ci
But right after that message I get the following one:
[error] o.h.t.h.SchemaUpdate - could not get database metadata
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'sequences' in information_schema
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_51]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_51]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_51]
at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[na:1.7.0_51]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) ~[mysql-connector-java-5.1.18.jar:na]
at com.mysql.jdbc.Util.getInstance(Util.java:386) ~[mysql-connector-java-5.1.18.jar:na]
[error] o.h.t.h.SchemaUpdate - could not complete schema update
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'sequences' in information_schema
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_51]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_51]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_51]
at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[na:1.7.0_51]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) ~[mysql-connector-java-5.1.18.jar:na]
at com.mysql.jdbc.Util.getInstance(Util.java:386) ~[mysql-connector-java-5.1.18.jar:na]
I don't quite understand the message and I have been searching for quite long and have not found anything that makes me understand what is happening.
Thanks in advance
Thank you #Krzysiek for the interest. I revised the persistence.xml again and found the error there. As I think you suspected it was about the defined dialect.
As I previously used h2, so the dialect was defined for that instead of for MySQL.
Here's the new persistence.xml which works fine, in case someone encounters the same problem as me.
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
I also would like to note for new users like me that if you want to create the tables from java code you should set to create-drop, and just to use and edit data stored you might want to switch it to "update" instead, when the database is created and will not suffer structural changes anymore.

Camunda Process Engine configuration with MockExpressionManager for wildfly

We have Camunda installed as a module for Wildfly8.
Everything works fine, but I need a way to mock java delegates for unit tests (with Arquillian).
As I understand, org.camunda.bpm.engine.test.mock.Mocks can be used to provide mocked delegates.
According to JavaDoc, I should register MockExpressionManager in my process engine configuration.
I've found some similar config with MockExpressingManager here
https://github.com/camunda/camunda-bpm-assert/blob/master/camunda-bpm-assert-examples/src/test/resources/camunda.cfg.xml
but camunda module for wildfly is configured in standalone-full.xml:
<subsystem xmlns="urn:org.camunda.bpm.jboss:1.1">
<process-engines>
<process-engine name="default" default="true">
<datasource>
java:jboss/datasources/ProcessEngine
</datasource>
<history-level>
full
</history-level>
<configuration>
org.camunda.bpm.engine.cdi.CdiJtaProcessEngineConfiguration
</configuration>
<properties>
<property name="jobExecutorAcquisitionName">
default
</property>
<property name="isAutoSchemaUpdate">
true
</property>
<property name="authorizationEnabled">
true
</property>
<property name="jobExecutorDeploymentAware">
true
</property>
<property name="expressionManager">
org.camunda.bpm.engine.test.mock.MockExpressionManager
</property>
</properties>
but this doesn't work, and on wildfly startup I see
16:45:31,930 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 61) MSC000001: Failed to start service org.camunda.bpm.platform.process-engine.default: org.jboss.msc.service.StartException in service org.camunda.bpm.platform.process-engine.default: org.camunda.bpm.engine.ProcessEngineException: Could not set value for property 'expressionManager' on class org.camunda.bpm.engine.cdi.CdiJtaProcessEngineConfiguration
at org.camunda.bpm.container.impl.jboss.service.MscManagedProcessEngineController$1.run(MscManagedProcessEngineController.java:97)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [rt.jar:1.8.0_11]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [rt.jar:1.8.0_11]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_11]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_11]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_11]
at org.jboss.threads.JBossThread.run(JBossThread.java:122) [jboss-threads-2.1.1.Final.jar:2.1.1.Final]
Caused by: org.camunda.bpm.engine.ProcessEngineException: Could not set value for property 'expressionManager' on class org.camunda.bpm.engine.cdi.CdiJtaProcessEngineConfiguration
at org.camunda.bpm.container.impl.metadata.PropertyHelper.applyProperty(PropertyHelper.java:76)
at org.camunda.bpm.container.impl.metadata.PropertyHelper.applyProperties(PropertyHelper.java:94)
at org.camunda.bpm.container.impl.jboss.service.MscManagedProcessEngineController.startProcessEngine(MscManagedProcessEngineController.java:173)
at org.camunda.bpm.container.impl.jboss.service.MscManagedProcessEngineController$2.run(MscManagedProcessEngineController.java:131)
at org.camunda.bpm.container.impl.jboss.service.MscManagedProcessEngineController$2.run(MscManagedProcessEngineController.java:129)
at org.camunda.bpm.container.impl.jboss.util.Tccl.runWithTccl(Tccl.java:53)
at org.camunda.bpm.container.impl.jboss.util.Tccl.runUnderClassloader(Tccl.java:45)
at org.camunda.bpm.container.impl.jboss.service.MscManagedProcessEngineController.startInternal(MscManagedProcessEngineController.java:129)
at org.camunda.bpm.container.impl.jboss.service.MscManagedProcessEngineController$1.run(MscManagedProcessEngineController.java:90)
... 6 more
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.8.0_11]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [rt.jar:1.8.0_11]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.8.0_11]
at java.lang.reflect.Method.invoke(Method.java:483) [rt.jar:1.8.0_11]
at org.camunda.bpm.container.impl.metadata.PropertyHelper.applyProperty(PropertyHelper.java:74)
... 14 more
How to set this MockExpressionManager to the configuration correctly?
Or may be there are some other ways to mock java delegates?
From looking at the Camunda code, they're expecting a reference to an expression manager to be passed in. If I had to guess, this is being passed in as a string. A dirty way I can think of to handle this is to create your subclass of CdiJtaProcessEngineConfiguration which configures the proper expression manager, deploy that JAR to their module and wire that in.
Personally, I found their configuration to be a bit too tight for me in direct wildfly integration and chose to configure their library in my app directly.

SQLException w/ Tomcat 7.0 JDBC connection pool and MySql

I'm trying to use the new Tomcat 7.0 JDBC connection pool and MySql. It says here that you can drop the latest tomcat-jdbc.jar into your tomcat/lib directory in a Tomcat 6.0 environment, and that's what I've done.
I'm using Spring to initialize and instantiate the DataSource per examples around the web:
<bean id="myDataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"
p:driverClassName="${driver_class}"
p:url="${url}"
p:username="${username?root}"
p:password="${password}"
p:initialSize="10"
p:initSQL="SELECT DTS FROM DT_TM_TS FOR READ ONLY WITH UR"
p:minIdle="10"
p:maxIdle="100"
p:maxActive="100"
p:maxWait="6000"
p:jmxEnabled="true"
p:jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
p:removeAbandoned="true"
p:removeAbandonedTimeout="60"
p:logAbandoned="true"
p:testOnBorrow="true"
p:testOnReturn="false"
p:testWhileIdle="false"
p:useEquals="false"
p:fairQueue="false"
p:timeBetweenEvictionRunsMillis="30000"
p:minEvictableIdleTimeMillis="30000"
p:validationInterval="1800000"
p:validationQuery="SELECT DTS FROM DT_TM_TS FOR READ ONLY WITH UR"
/>
However when I try to connect to the db it throws this exception:
java.sql.SQLException: "com.mysql.jdbc.Driver"
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:243)
at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:176)
at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:647)
at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:589)
at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:452)
at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:130)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:99)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:110)
at org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection(InjectedDataSourceConnectionProvider.java:46)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:111)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2101)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1325)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:132)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:225)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:308)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findDefaultEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:529)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:495)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:656)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:629)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:147)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4135)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4630)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)
at org.apache.catalina.core.StandardService.start(StandardService.java:519)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: java.lang.ClassNotFoundException: "com.mysql.jdbc.Driver"
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:236)
... 59 more
I've got mysql-connector-java-5.1.13-bin.jar in both the webapp WEB-INF/lib and tomcat/lib. I can connect fine specifying the driver details in persistence.xml (no DataSource/connection pooling).
There is actually a very simple workaround with Spring. Instead of having the Tomcat connection pool create the database connection and fail because of class loader shenanigans, let Spring create it and pass a reference to the connection pool.
<!-- Data source template for use in the connection pool. -->
<bean id="dataSourceTemplate" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- Connection pool as data source. -->
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<!-- Refer to a separately created bean as a data source template to work around a quirk of Tomcat's class loader. -->
<property name="dataSource" ref="dataSourceTemplate" />
...
</bean>
Then the connection pool will actually use the database connection as a template for creating as many additional connections as it needs.
The best part is that you don't have to mess with tomcat/lib, which you may not even have access to.
I'm using Spring 3.1, Hibernate 4.0, VMware vFabric tc Server Developer Edition 2.6.1 with Tomcat 7.0.20.B and Maven 3. Having the connector managed by Maven I encountered the same issue.
Infact reading the manual I've found
driverClassName
(String) The fully qualified Java class name of the
JDBC driver to be used. The driver has to be accessible from the same
classloader as tomcat-jdbc.jar
So I resolved by adding the mysql connector (mysql-connector-java-5.1.18-bin.jar) in the lib directory of Tomcat (vfabric-tc-server-developer-2.6.1.RELEASE/tomcat-7.0.20.B.RELEASE/lib) and setting in Maven the scope of the connector package to provided.
In Tomcat 6.0 I need to put my JDBC drivers into common/lib/ext
But the search path for that is configured in catalina.properties check out the property common.loader in there. Make sure that common/lib is listed there.

Categories

Resources