【发布时间】:2015-07-26 17:02:21
【问题描述】:
我是否需要在 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 服务器控制台的连接池中指定远程连接? 还是我错过了其他东西?
【问题讨论】:
标签: java mysql jakarta-ee jpa glassfish