user2100776
user2100776

Reputation: 81

Why can't I use the embedded EJB container with my own persistence unit?

I want to use junit in my project, but haven't been able to do so because I always get the following exception:

SEVERE: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method
Apr 15, 2013 11:03:43 PM org.glassfish.api.ActionReport failure

SEVERE: Exception while preparing the app
Apr 15, 2013 11:03:43 PM com.sun.enterprise.v3.server.ApplicationLifecycle deploy

SEVERE: Invalid resource : mysql/myapp__pm
java.lang.RuntimeException: Invalid resource : mysql/myapp__pm

For my project I'm using Hibernate with a MySQL database located on a virtual machine, and right now the project works fine with it. I had a similar problem when I configured the connection for the project the first time, however I solved the problem after I found out how to make Netbeans generate the adequate resources for me. However, this shouldn't be a problem now since I'm trying to use the same persistence unit which already works and has all the needed resources (including the connection pool in glassfish). Additionally, why does it append a __pm at the end?

Also, I have seen some posts in which people use OpenEJB for testing. Would it be easier to use OpenEJB (only for testing) instead of using the embedded container from my glassfish installation?

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="myapp-ejb_ejb_1.0-SNAPSHOTPU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>mysql/myapp</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
    </properties>
  </persistence-unit>
</persistence>

glassfish-resources.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
    <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="mysql_myapp_rootPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
        <property name="serverName" value="myapp_servername"/>
        <property name="portNumber" value="3306"/>
        <property name="databaseName" value="myapp"/>
        <property name="User" value="******"/>
        <property name="Password" value="*********"/>
        <property name="URL" value="jdbc:mysql://myapp_servername:3306/myapp?zeroDateTimeBehavior=convertToNull"/>
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    </jdbc-connection-pool>
    <jdbc-resource enabled="true" jndi-name="mysql/myapp" object-type="user" pool-name="mysql_myapp_rootPool"/>
</resources>

Upvotes: 0

Views: 1689

Answers (3)

Ahmad Abdelghany
Ahmad Abdelghany

Reputation: 13198

Basically, when you use the glassfish embedded EJB Container you need to remember that this is a stand alone container that needs to have resources of its own (most importantly data sources)

So the question becomes, how to define data sources for my embedded ejb container? Simply make your embedded ejb container point to domain.xml file where you have data sources defined.

Map<String, Object> properties = new HashMap<String, Object>();

properties.put(
    "org.glassfish.ejb.embedded.glassfish.configuration.file",
    "C:/Users/Ahmad/AppData/Roaming/NetBeans/8.0.1/config/GF_4.1/domain1/config/domain.xml"
);
EJBContainer container = javax.ejb.embeddable.EJBContainer.createEJBContainer(properties);

It would recommend though to make a separate data source for testing and to put the domain.xml containing that data source somewhere in your tests/resources package

Upvotes: 1

Gab
Gab

Reputation: 8323

Have a look to Arquillian to set up a container for testing purpose.

You can use both glassfish arquillian embedded container or your installed glassfish container with associated configuration.

Upvotes: 0

Pradeep Pati
Pradeep Pati

Reputation: 5919

You need to create a ConnectionPool in GlassFish using the Datasource, and use the JNDI of the connection pool to access DB. That can be done by following the guide here.

Upvotes: 0

Related Questions