提问者:小点点

在GlassFish中为远程数据库创建JDBC连接池


我需要在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 数据源资源管理器视图(数据工具平台)和 MySQL 工作台查看表时,没有要查看的表(是的,我已经刷新了)。这让我相信数据被存储在其他地方,就像存储在默认位置一样。我打开 GlassFish 开发者控制台 (本地主机:4848)

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服务器控制台的连接池中指定远程连接吗?还是我错过了什么?


共1个答案

匿名用户

您正在持久性. xml中设置JTA事务类型,这意味着应用程序服务器将管理数据库连接,您只需要提供应用程序服务器中配置的数据源的JNDI名称。

如果需要使用 javax.persistence.jdbc.* 属性在持久性.xml文件中设置数据源,则需要切换到 RESOURCE-LOCAL 事务类型,在这种情况下,您还需要管理应用程序中的事务(不确定是否需要这样做)。

有关其他信息,您可以参考Java EE文档。