简体   繁体   English

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

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

Do I need to create a connection pool for a remote database in my GlassFish server console? 是否需要在GlassFish服务器控制台中为远程数据库创建连接池?

Details: 细节:

I'm using a MySQL Amazon RDS instance for my database. 我正在为数据库使用MySQL Amazon RDS实例。 I have the database setup and running from the AWS console. 我已经在AWS控制台上设置并运行了数据库。 Using the Eclipse data source tools, pings succeed. 使用Eclipse数据源工具,ping操作成功。 I'm using EclipseLink as my JPA provider. 我正在使用EclipseLink作为我的JPA提供程序。 My peristence.xml file looks like this: 我的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>

The MySQL connector (connector/j) is placed in my application build path. MySQL连接器(connector / j)放在我的应用程序构建路径中。 Within my application, I can (rudimentary) test the connection (which also succeeds): 在我的应用程序中,我可以(基本)测试连接(也可以成功):

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) {}
        }

Since, I'm running in a container managed environment, I can even test that the injected EntityManager is connected properly: 由于我正在容器管理的环境中运行,因此我什至可以测试注入的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());
}

Which outputs the following: 输出以下内容:

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

So, it looks to me that the connection should be good and work. 因此,在我看来,连接应该很好并且可以正常工作。 However, when attempting to view the tables using both Eclipse Data Source Explorer View (Data Tools Platform) and the MySQL Workbench, there are no tables to view (yes I've refreshed). 但是,当尝试同时使用Eclipse Data Source Explorer View(数据工具平台)和MySQL Workbench查看表时,没有要查看的表(是的,我已经刷新了)。 This led me to believe that the data was being stored elsewhere, as if to a default location. 这使我相信数据被存储在其他位置,就像存储在默认位置一样。 I open GlassFish developer console (localhost:4848) > JDBC > Connection Pools . 我打开GlassFish开发人员控制台(localhost:4848)> JDBC>连接池 There is a "default" connection pool which I deleted. 我删除了一个“默认”连接池。 Now, when running the app I get the following errors at the beginning of the stack trace: 现在,在运行应用程序时,在堆栈跟踪的开头出现以下错误:

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 

Yet, I never specified for the server to connect to jdbc/__default__pm . 但是,我从未指定服务器连接到jdbc/__default__pm This leads me to believe that it looks for a "default" connection pool as a fallback if it can't connect to the specified one in the persistence.xml file. 这使我相信,如果无法连接到persistence.xml文件中的指定连接池,它将寻找“默认”连接池作为备用。 Though, the connection works it seems it is not being made by the app, is this because I have to specify the remote connection in a connection pool in the GlassFish server console? 虽然,连接似乎没有由应用程序建立,但这是因为我必须在GlassFish服务器控制台的连接池中指定远程连接吗? Or am I missing something else? 还是我想念其他东西?

You are setting up JTA transaction-type in your persistence.xml, this means that the application server will manage the database connection and you only need to provide with the JNDI name of the data source configured in the application server. 您正在persistence.xml中设置JTA事务类型,这意味着应用程序服务器将管理数据库连接,并且只需要提供在应用程序服务器中配置的数据源的JNDI名称。

If you need to set up the data source in the persistence.xml file using javax.persistence.jdbc.* properties, you will need to switch to RESOURCE-LOCAL transaction-type, in which case you will need to also manage transactions in the application (not sure if this is desired). 如果您需要使用javax.persistence.jdbc。*属性在persistence.xml文件中设置数据源,则需要切换到RESOURCE-LOCAL事务类型,在这种情况下,您还需要在应用程序(不确定是否需要)。

You may refer to the Java EE documentation for additional information. 您可以参考Java EE文档以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM