EclipseLink-4021 - 无法从驱动程序获取连接

时间:2016-07-01 10:02:15

标签: mysql tomcat jpa servlets eclipselink

我经常遇到异常 EclipseLink-4021"当我的应用程序启动时,无法从驱动程序[null],用户[null]和URL [null]" 获取连接

这意味着Eclipselink没有找到定义为连接MySQL的persistence.xml。 我的persistence.xml在文件夹/ WEB-INF / classes中,我认为这是标准的地方。

当我一次又一次地启动tomcat时,它终于有效了。 当一切运行良好时,我会得到这个日志。

[EL Info]: 2016-07-01 11:22:55.268--ServerSession(975412652)--EclipseLink, version: Eclipse Persistence Services - 2.6.3.v20160428-59c81c5
[EL Info]: connection: 2016-07-01 11:22:56.794--ServerSession(975412652)--/file:/D:/Workspace_Eclipse/xxxWebapp/target/classes/_db1 login successful

我认为这可能来自我的WebApp架构,但我不明白出了什么问题。

我的WebApp是使用Java 8,ZK 8,EclipseLink 2.6.3,MySQL 5.7.10

开发的

在init处调用HttpServlet(GlobalEnvProperties.java)来准备与其他数据库和石英调度程序的连接之类的事情。

web.xml完整内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd"
    version="2.4">
    <description>app</description>
    <display-name>Webapp</display-name>
    <servlet>
        <description>ZK loader for ZUML pages</description>
        <servlet-name>zkLoader</servlet-name>
        <servlet-class>org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>
        <init-param>
            <param-name>update-uri</param-name>
            <param-value>/zkau</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>zkLoader</servlet-name>
        <url-pattern>*.zul</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>zkLoader</servlet-name>
        <url-pattern>*.zhtml</url-pattern>
    </servlet-mapping>
    <servlet>
        <description>The asynchronous update engine for ZK</description>
        <servlet-name>auEngine</servlet-name>
        <servlet-class>org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>auEngine</servlet-name>
        <url-pattern>/zkau/*</url-pattern>
    </servlet-mapping>
    <listener>
        <description>ZK listener for session cleanup</description>
        <listener-class>org.zkoss.zk.ui.http.HttpSessionListener</listener-class>
    </listener>
    <session-config>
        <session-timeout>14400</session-timeout>
    </session-config>   
    <servlet>
        <description>Gestion des environnements</description>
        <servlet-name>GlobalEnvProperties</servlet-name>
        <servlet-class>com.company.app.webapp.common.GlobalEnvProperties</servlet-class>
        <load-on-startup>3</load-on-startup>
    </servlet>    
    <mime-mapping>
        <extension>doc</extension>
        <mime-type>application/vnd.ms-word</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>gif</extension>
        <mime-type>image/gif</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>htm</extension>
        <mime-type>text/html</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>html</extension>
        <mime-type>text/html</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>jpeg</extension>
        <mime-type>image/jpeg</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>jpg</extension>
        <mime-type>image/jpeg</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>js</extension>
        <mime-type>text/javascript</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>pdf</extension>
        <mime-type>application/pdf</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>png</extension>
        <mime-type>image/png</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>txt</extension>
        <mime-type>text/plain</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>xls</extension>
        <mime-type>application/vnd.ms-excel</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>xml</extension>
        <mime-type>text/xml</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>zhtml</extension>
        <mime-type>text/html</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>zul</extension>
        <mime-type>text/html</mime-type>
    </mime-mapping>
    <welcome-file-list>
        <welcome-file>auth.zul</welcome-file>
    </welcome-file-list>
</web-app>

在servlet GlobalEnvProperties的init void中,我使用 Persistence.createEntityManagerFactory(&#34; db1&#34;)

获取EntityManager
public void init() throws ServletException {
        bddFactories = new HashMap<String, EntityManagerFactory>();
        entityManagers = new HashMap<String, EntityManager>();
        //global properties
        configProp = new Properties();
        InputStream ficProperties = Thread.currentThread().getContextClassLoader().getResourceAsStream("META-INF/app.properties");
        try {
            configProp.load(new InputStreamReader(ficProperties, "UTF-8"));
        } catch (IOException e) {
            CustomLogger.info("Unable to open properties file");
            CustomLogger.tracerErreurFatale(e);
        }

        /*
         * BDD connection
         */    
        // Base db1
        try {
            final int sleepChargementPersitence = 10000;
            Thread.sleep(sleepChargementPersitence);
            bddFactories.put("db1", Persistence.createEntityManagerFactory("db1"));
        } catch (javax.persistence.PersistenceException | DatabaseException e) {
            CustomLogger.info("Unable to connect db1");
            CustomLogger.tracerErreurFatale(e);
        } catch (InterruptedException e) {
            CustomLogger.info("Sleep failed");
        }

        OtherConnectionsLoad();

        //scheduler 
  try {
            CustomLogger.debug("Scheduler start");
            DailyTreatmentScheduler.main(null);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

我的persistence.xml内容:

?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    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">
    <persistence-unit name="db1">
        <class>com.company.app.model.Action</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <shared-cache-mode>NONE</shared-cache-mode>
        <properties>                                              
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="*******" />
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url"
                value="jdbc:mysql://server:3306/db1?useSSL=false" />            
            <property name="eclipselink.deploy-on-startup" value="true" />
            <property name="javax.persistence.jdbc.initialSize" value="2" />
            <property name="javax.persistence.jdbc.maxActive" value="10" />
            <property name="javax.persistence.logging.level" value="INFO" />
            <property name="eclipselink.query-results-cache" value="false" />
            <property name="javax.persistence.refresh" value="true" />
        </properties>
    </persistence-unit>
</persistence>

我希望有人可以解释我错在哪里。 感谢那些试图了解我的问题的人。

0 个答案:

没有答案