在GlassFish中为远程数据库

时间:2015-05-14 22:00:56

标签: java mysql java-ee jpa glassfish

我是否需要在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)&gt; JDBC&gt;连接池。我删除了一个“默认”连接池。现在,在运行应用程序时,我在堆栈跟踪的开头出现以下错误:

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 个答案:

答案 0 :(得分:0)

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

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

您可以参考Java EE documentation了解更多信息。

相关问题