我需要为我的GlassFish服务器控制台中的远程数据库创建连接池吗?在GlassFish中为远程数据库创建JDBC连接池
详情:
我使用的是MySQL亚马逊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的连接器(连接器/ 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视图(数据工具平台)和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服务器控制台的连接池中指定远程连接?还是我缺少别的东西?