Reputation:
I am not able to find ecbDB JTA data source in the unit test.
For RESOURCE_LOCAL the same persistence.xml works. Only within an EJB container it fails to locate a JTA data source.
I get the following exception when the unit test is executed..
SEVERE: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method
java.lang.RuntimeException: javax.naming.NamingException: Lookup failed for 'ecbDB' in SerialContext [Root exception is javax.naming.NameNotFoundException: ecbDB not found]
at org.glassfish.persistence.jpa.PersistenceUnitInfoImpl.<init>(PersistenceUnitInfoImpl.java:111)
Here is persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="ecbPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>ecbDB</jta-data-source>
<mapping-file>META-INF/orm.xml</mapping-file>
<properties>
<property
name="javax.persistence.jdbc.driver"
value="org.apache.derby.jdbc.EmbeddedDriver" />
<property
name="javax.persistence.jdbc.url"
value="jdbc:derby:target/ecbDB;create=true" />
<!-- EclipseLink 2.0 properties -->
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="sql-script" />
<!-- <property name="eclipselink.ddl-generation.output-mode" value="database"
/> -->
<property name="eclipselink.application-location" value="./target/ddl" />
<property name="eclipselink.create-ddl-jdbc-file-name"
value="create.sql" />
<property name="eclipselink.drop-ddl-jdbc-file-name" value="drop.sql" />
<property name="eclipselink.logging.level" value="ALL" />
<property name="eclipselink.logging.file" value="./target/eclipselink.logs" />
</properties>
</persistence-unit>
</persistence>
Upvotes: 6
Views: 18041
Reputation: 8219
Here is a step-by-step description I have used to set up JTA based connection with EclipseLink.
First, make sure that:
GlassFish server is up and running
asadmin list-domains
Apache Derby server is up and running
NetworkServerControl.bat sysinfo -h localhost -p 1527
Next, create JDBC Connection Pool and JDBC Resource
org.apache.derby.jdbc.EmbeddedDataSource
asadmin create-jdbc-connection-pool
--datasourceclassname org.apache.derby.jdbc.EmbeddedDataSource
--restype javax.sql.XADataSource
--property databaseName=ecbDB:serverName=localhost:portNumber=1527:password=APP: user=APP:connectionAttributes=\;create\=true EmbeddedPool
asadmin create-jdbc-resource --connectionpoolid EmbeddedPool jdbc/EmbeddedResource
If you take a look at GlassFish Administration Console (localhost:4848) you will notice new nodes:
Resources > JDBC > JDBC Connection Pools > EmbeddedPool
Resources > JDBC > JDBC Resources > jdbc/EmbeddedResource
As you can see jdbc/EmbeddedResource
has been assigned to EmbeddedPool
.
Then, create a minimal version of persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="ecbPU">
<jta-data-source>jdbc/EmbeddedResource</jta-data-source>
<properties>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
</persistence>
Use stateless/stateful session beans as components to manage persistence operations (as far as I know this is the preferred strategy for Java EE applications):
public interface ECBService {
}
@Stateless
public class ECBServiceBean implements ECBService {
@PersistenceContext(unitName = "ecbPU")
protected EntityManager em;
}
Use setter injection to assign environment naming context dependency into a field (useful in case of unit testing):
public class ECBServlet extends HttpServlet {
private ECBService service;
@EJB
public void setECBService(EcbService service) {
this.service = service;
}
}
Finally, deployment produces the following:
INFO: EclipseLink, version: Eclipse Persistence Services - 2.5.0.v20130507
FINE: Detected database platform: org.eclipse.persistence.platform.database.JavaDBPlatform
CONFIG: connecting(DatabaseLogin(
platform=>DatabasePlatform
user name=> ""
connector=>JNDIConnector datasource name=>null
))
CONFIG: Connected: jdbc:derby:ecbDB
User: APP
Database: Apache Derby Version: 10.10.1.1 - (1458268)
Driver: Apache Derby Embedded JDBC Driver Version: 10.10.1.1 - (1458268)
CONFIG: connecting(DatabaseLogin(
platform=>JavaDBPlatform
user name=> ""
connector=>JNDIConnector datasource name=>null
))
CONFIG: Connected: jdbc:derby:ecbDB
User: APP
Database: Apache Derby Version: 10.10.1.1 - (1458268)
Driver: Apache Derby Embedded JDBC Driver Version: 10.10.1.1 - (1458268)
I hope it helps.
Upvotes: 2
Reputation: 1487
Are you running this under embedded Glassfish? If so the problem you are is seeing is because he has a JTA datasource ecbDB is not defined in your domain.xml. In that case Kaster's answer is on the right track and you should create your embedded connection pool and associated JDBC resource. You can use the asadmin command or Glassfish web admin GUI. At that point you should also remove the two javax.persistence.jdbc properties from your persistence.xml because those settings will be convered by your connection pool resource settings.
Upvotes: 0
Reputation: 89
First make sure your derby configuration have a suitable JDBC resource (with the shell command "asadmin list-jdbc-resources"). If not, make one with "asadmin create-jdbc-resource". For help type "asadmin list-commands".
Upvotes: 0