簡體   English   中英

在GlassFish中為遠程數據庫創建JDBC連接池

[英]Create JDBC connection pool in GlassFish for remote database

是否需要在GlassFish服務器控制台中為遠程數據庫創建連接池?

細節:

我正在為數據庫使用MySQL Amazon RDS實例。 我已經在AWS控制台上設置並運行了數據庫。 使用Eclipse數據源工具,ping操作成功。 我正在使用EclipseLink作為我的JPA提供程序。 我的peristence.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="WebApplication" transaction-type="JTA">
    <properties>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"></property>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://link-to-my-database.amazonaws.com:3306/TestDB"></property>
        <property name="javax.persistence.jdbc.user" value="admin"/>
        <property name="javax.persistence.jdbc.password" value="my-password"/>
        <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
        <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
    </properties>
</persistence-unit>

MySQL連接器(connector / j)放在我的應用程序構建路徑中。 在我的應用程序中,我可以(基本)測試連接(也可以成功):

try{
            System.out.println("Loading driver...");
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Driver loaded!");
        }catch(ClassNotFoundException e){
            System.out.println("JAR file containing JDBC driver (com.mysql.jdbc.Driver) has not been placed in the class path.");
            e.printStackTrace();
        }
        Connection connection = null;
        try{
            System.out.println("Connecting to database...");
            connection = DriverManager.getConnection("jdbc:mysql://link-to-my-database.us-east-1.rds.amazonaws.com:3306/TestDB", "admin", "my-password");
            System.out.println("Connected to database!");
            System.out.println("Connection read only: " + connection.isReadOnly());
            System.out.println("Connection warnings: " + connection.getWarnings()); 
            System.out.println("Connection schema: " + connection.getSchema());
            System.out.println("MetaData: " + connection.getMetaData().toString());
        }catch(SQLException e){
            System.out.println("Error connecting to the database. SQLException:");
            e.printStackTrace();
            System.out.println("Error connecting to the database. RuntimeException:");
            throw new RuntimeException("Cannot connect the database!", e);
        }finally{
            System.out.println("Closing the connection.");
            if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
        }

由於我正在容器管理的環境中運行,因此我什至可以測試注入的EntityManager是否正確連接:

@PersistenceContext(unitName = "WebApplication")
private EntityManager em;

System.out.println("CONNECTION PROPERTIES:");
Map<String, Object> props = em.getProperties();
for(Map.Entry<String, Object> prop : props.entrySet()){
    System.out.println("Property: " + prop.getKey() + " Value: " + prop.getValue());
}

輸出以下內容:

2015-05-14T16:23:44.320-0400|Info: CONNECTION PROPERTIES:
2015-05-14T16:23:44.320-0400|Info: Property: eclipselink.ddl-generation Value: drop-and-create-tables
2015-05-14T16:23:44.320-0400|Info: Property: javax.persistence.jdbc.url Value: jdbc:mysql://link-to-my-database.us-east-1.rds.amazonaws.com:3306/TestDB
2015-05-14T16:23:44.320-0400|Info: Property: javax.persistence.jdbc.user Value: admin
2015-05-14T16:23:44.320-0400|Info: Property: javax.persistence.jdbc.driver Value: com.mysql.jdbc.Driver
2015-05-14T16:23:44.320-0400|Info: Property: javax.persistence.jdbc.password Value: password
2015-05-14T16:23:44.320-0400|Info: Property: javax.persistence.schema-generation.database.action Value: drop-and-create
2015-05-14T16:23:44.320-0400|Info: Property: hibernate.transaction.manager_lookup_class Value: org.hibernate.transaction.SunONETransactionManagerLookup
2015-05-14T16:23:44.320-0400|Info: Property: eclipselink.target-server Value: SunAS9
2015-05-14T16:23:44.320-0400|Info: Property: toplink.target-server Value: SunAS9

因此,在我看來,連接應該很好並且可以正常工作。 但是,當嘗試同時使用Eclipse Data Source Explorer View(數據工具平台)和MySQL Workbench查看表時,沒有要查看的表(是的,我已經刷新了)。 這使我相信數據被存儲在其他位置,就像存儲在默認位置一樣。 我打開GlassFish開發人員控制台(localhost:4848)> JDBC>連接池 我刪除了一個“默認”連接池。 現在,在運行應用程序時,在堆棧跟蹤的開頭出現以下錯誤:

2015-05-14T17:06:27.493-0400|Severe: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method
2015-05-14T17:06:27.516-0400|Severe: Exception while preparing the app
2015-05-14T17:06:27.517-0400|Severe: Exception during lifecycle processing
java.lang.RuntimeException: Invalid resource : jdbc/__default__pm 

但是,我從未指定服務器連接到jdbc/__default__pm 這使我相信,如果無法連接到persistence.xml文件中的指定連接池,它將尋找“默認”連接池作為備用。 雖然,連接似乎沒有由應用程序建立,但這是因為我必須在GlassFish服務器控制台的連接池中指定遠程連接嗎? 還是我想念其他東西?

您正在persistence.xml中設置JTA事務類型,這意味着應用程序服務器將管理數據庫連接,並且只需要提供在應用程序服務器中配置的數據源的JNDI名稱。

如果您需要使用javax.persistence.jdbc。*屬性在persistence.xml文件中設置數據源,則需要切換到RESOURCE-LOCAL事務類型,在這種情況下,您還需要在應用程序(不確定是否需要)。

您可以參考Java EE文檔以獲取更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM