0

I have a JBoss AS, and on this server there is a standalone.xml file where there several properties, there are my datasources too, so how combine the datasources in the standalone.xml file with a persistence unit that I want to add to an EJB ?

1 Answers1

0

Just add <jta-data-source>java:/ExampleDS</jta-data-source> providing your datasource jndi-name to the persistence.xml.

Example of Datasource:

<datasource jndi-name="java:/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
                    <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
                    <driver>h2</driver>
                    <security>
                        <user-name>sa</user-name>
                        <password>sa</password>
                    </security>
                </datasource>

Example of persistence.xml referencing datasource ExampleDS:

<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="example">
      <jta-data-source>java:/ExampleDS</jta-data-source>
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
         <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
      </properties>
   </persistence-unit>
</persistence>

Example injecting Persistence Unit in your EJB3:

@Stateless
public class MyEJB {

    @PersistenceContext(unitName="example") protected EntityManager entityManager;

    public void createEmployee(String fName, String lName) {
        Employee employee  = new Employee();
        employee.setFirstName(fName);
        employee.setLastName(lName);
        entityManager.persist(employee);
    }
...
}
Elvis Rocha
  • 104
  • 3
  • Should I include in my package the MySQL driver connector ? – mobile_beginner_2015 Dec 01 '16 at 23:42
  • The best way is configure a module in JBoss AS. To do that just follow that guide: https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Application_Platform/6/html/Administration_and_Configuration_Guide/Install_a_JDBC_Driver_as_a_Core_Module1.html – Elvis Rocha Dec 02 '16 at 00:19