Reputation: 11
I was wondering if someone could help me with tomcat (most likely) configuration issue.
I am using tomcat 8/eclipse Mars/h2 database. Built a project (sample standalone application that writes to h2) and it is working fine (I can see that the rows get added to the database). However when I try to use the same steps in my web application - I'm getting an exception.
Before posting the question here I read these links: http://j2stuff.blogspot.com/2012/11/setup-datasource-for-h2-and-postgres-on.html Configuring Liberty Profile to use H2 database https://keithrieck.wordpress.com/2010/01/28/h2-database-on-tomcat/ (and many more :-( ) but still can't make my application work.
Here is the call stack.
Jun 07, 2016 10:17:16 AM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
name: h2
...]
Jun 07, 2016 10:17:16 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Jun 07, 2016 10:17:16 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [null] at URL [jdbc:h2:tcp://localhost/~/h2db]
Jun 07, 2016 10:17:16 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=sa}
Jun 07, 2016 10:17:16 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Jun 07, 2016 10:17:16 AM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Jun 07, 2016 10:17:16 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [com.cerner.cloud.setup.JerseyApplication] in context with path [/person-search-service] threw exception [A MultiException has 4 exceptions. They are:
1. javax.persistence.PersistenceException: Unable to build entity manager factory
2. java.lang.IllegalStateException: Unable to perform operation: create on com.cerner.cloud.service.InMemoryPersonServiceImpl
3. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.cerner.cloud.resource.PersonResource errors were found
4. java.lang.IllegalStateException: Unable to perform operation: resolve on com.cerner.cloud.resource.PersonResource
] with root cause
java.sql.SQLException: No suitable driver found for jdbc:h2:tcp://localhost/~/h2db
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:208)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionCreator.makeConnection(DriverManagerConnectionCreator.java:34)
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:106)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:40)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:19)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:138)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:110)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:74)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
Here is the section from context.xml (in eclipse -> Servers)
<ResourceLink name="jdbc/h2"
auth="Container"
type="javax.sql.DataSource"/>
Here is the section from server.xml (in Eclipse -> Servers)
<Resource name="h2" auth="Container"
global = "h2"
type="javax.sql.DataSource"
driverClassName="org.h2.Driver"
url="jdbc:h2:localhost:8082/h2DB"
username="sa" password=""
maxActive="20" maxIdle="10" maxWait="-1"
description="Datasource to person database" />
Here is my persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<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_1_0.xsd" version="1.0">
<persistence-unit name="h2" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- <class>Entity.Person</class> -->
<properties>
<property name="connection.driver_class" value="org.h2.Driver"/>
<property name="hibernate.connection.url" value="jdbc:h2:tcp://localhost/~/h2db"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<!-- <property name="hibernate.hbm2ddl.auto" value="create-drop"/> -->
<property name="hibernate.show_sql" value="false" />
<property name="javax.persistence.jdbc.user" value="sa" />
</properties>
</persistence-unit>
</persistence>
And here is the code that is working in stand alone application that i'm trying to call from a web application
public class DBTalkImplementation {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("h2");
public DBTalkImplementation()
{
System.out.println("In default constructor");
//Class.forName("org.h2.Driver");
}
//Function to add row to the database (person table)
public void AddPerson(int AssociateID, String First, String Last)
{
System.out.println("inAddPerson");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
System.out.println("DBTalkImplementation.CreatePerson");
Person _person = new Person(AssociateID,First,Last);
System.out.println(_person.toString());
em.persist(_person);
tx.commit();
em.close();
}
and finally - this is the function in my web application that is failing
@Override
public void addPerson(Person person) {
System.out.println("addPerson "+person.getFirstName());
DBTalkImplementation DB = new DBTalkImplementation();
DB.AddPerson(person.getId(), person.getFirstName(), person.getLastName());
dataStore.put(Integer.valueOf(person.getId()), person);
}
Any help with the issue or at least suggestion on the next steps is greatly appreciated.
Upvotes: 0
Views: 1035
Reputation: 11
Finally figured it out. 2 solutions. 1) Add the jar file explicitly (in eclipse run configurations -> choose the tomcat server -> classpath -> add external jar
or
2) add this line before you create entityManagerFactory
try
{
Class.forName("org.h2.Driver");
}
catch (Exception e)
{}
Upvotes: 1