简体   繁体   English

通过apache tomcat 8运行时无法运行h2数据库查询

[英]Can't run h2 database query when running through apache tomcat 8

I was wondering if someone could help me with tomcat (most likely) configuration issue. 我想知道是否有人可以帮助我解决tomcat(最有可能)的配置问题。

I am using tomcat 8/eclipse Mars/h2 database. 我正在使用tomcat 8 / eclipse Mars / h2数据库。 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). 构建了一个项目(写入h2的示例独立应用程序示例),并且工作正常(我可以看到这些行已添加到数据库中)。 However when I try to use the same steps in my web application - I'm getting an exception. 但是,当我尝试在Web应用程序中使用相同的步骤时-我遇到了异常。

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. 在此处发布问题之前,我已阅读以下链接: http : //j2stuff.blogspot.com/2012/11/setup-datasource-for-h2-and-postgres-on.html 配置自由配置文件以使用H2数据库 https:// keithrieck.wordpress.com/2010/01/28/h2-database-on-tomcat/ (还有更多:-(),但仍然无法使我的应用程序正常工作。

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) 这是context.xml中的部分(在eclipse-> Servers中)

<ResourceLink name="jdbc/h2"
  auth="Container"
  type="javax.sql.DataSource"/>

Here is the section from server.xml (in Eclipse -> Servers) 这是server.xml中的部分(在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 这是我的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 这是我正在尝试从Web应用程序调用的独立应用程序中运行的代码

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 最后-这是我的Web应用程序中失败的功能

@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. 非常感谢您对此问题的帮助或至少对下一步的建议。

Finally figured it out. 终于想通了。 2 solutions. 2个解决方案。 1) Add the jar file explicitly (in eclipse run configurations -> choose the tomcat server -> classpath -> add external jar 1)显式添加jar文件(在Eclipse运行配置中->选择tomcat服务器->类路径->添加外部jar

or 要么

2) add this line before you create entityManagerFactory 2)在创建entityManagerFactory之前添加此行

try
        {
        Class.forName("org.h2.Driver");
        }

        catch (Exception e)
        {} 

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

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