通过apache tomcat 8

时间:2016-06-07 15:43:45

标签: java eclipse tomcat

我想知道是否有人可以帮助我解决tomcat(最有可能)的配置问题。

我正在使用tomcat 8 / eclipse Mars / h2数据库。构建了一个项目(写入h2的示例独立应用程序),它工作正常(我可以看到行被添加到数据库中)。但是,当我尝试在我的Web应用程序中使用相同的步骤时 - 我得到了一个例外。

在此处发布问题之前,请阅读以下链接: 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/ (以及更多:-()但仍然无法使我的应用程序正常工作。

这是调用堆栈。

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)

以下是来自context.xml的部分(在eclipse - &gt; Servers中)

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

以下是server.xml(在Eclipse - &gt; 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"  />

这是我的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>

以下是我试图从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();
    }

最后 - 这是我的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);
    }

非常感谢任何有关该问题的帮助或至少对后续步骤的建议。

1 个答案:

答案 0 :(得分:0)

终于明白了。 2解决方案。 1)显式添加jar文件(在eclipse运行配置中 - &gt;选择tomcat服务器 - &gt; classpath - &gt;添加外部jar

2)在创建entityManagerFactory

之前添加此行
try
        {
        Class.forName("org.h2.Driver");
        }

        catch (Exception e)
        {}