java - Create JDBC connection pool in GlassFish for remote database -
do need create connection pool remote database in glassfish server console?
details:
i'm using mysql amazon rds instance database. have database setup , running aws console. using eclipse data source tools, pings succeed. i'm using eclipselink jpa provider. peristence.xml
file looks this:
<?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) placed in application build path. within application, can (rudimentary) test connection (which 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 class path."); e.printstacktrace(); } connection connection = null; try{ system.out.println("connecting 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 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 database. sqlexception:"); e.printstacktrace(); system.out.println("error connecting database. runtimeexception:"); throw new runtimeexception("cannot connect database!", e); }finally{ system.out.println("closing connection."); if (connection != null) try { connection.close(); } catch (sqlexception ignore) {} }
since, i'm running in container managed environment, can test injected entitymanager
connected properly:
@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 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, looks me connection should , work. however, when attempting view tables using both eclipse data source explorer view (data tools platform) , mysql workbench, there no tables view (yes i've refreshed). led me believe data being stored elsewhere, if default location. open glassfish developer console (localhost:4848) > jdbc > connection pools. there "default" connection pool deleted. now, when running app following errors @ beginning of 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 app 2015-05-14t17:06:27.517-0400|severe: exception during lifecycle processing java.lang.runtimeexception: invalid resource : jdbc/__default__pm
yet, never specified server connect jdbc/__default__pm
. leads me believe looks "default" connection pool fallback if can't connect specified 1 in persistence.xml
file. though, connection works seems not being made app, because have specify remote connection in connection pool in glassfish server console? or missing else?
you setting jta transaction-type in persistence.xml, means application server manage database connection , need provide jndi name of data source configured in application server.
if need set data source in persistence.xml file using javax.persistence.jdbc.* properties, need switch resource-local transaction-type, in case need manage transactions in application (not sure if desired).
you may refer java ee documentation additional information.
Comments
Post a Comment