Enterprise Java Development@TOPIC@
Physical connections to database are placed into a pool
Logical connections are borrowed and returned from/to the pool
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
<datasources>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password></password>
</security>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
</subsystem>
persistence.xml
Two transaction types -- one is primary path
No connection information - using pool from jta-data-source
Properties included define how to work with DB -- not how to connect
Likely the only transaction-type used
<?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="ejbjpa-hotel" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
...
<properties>
<property name="hibernate.dialect" value="${hibernate.dialect}"/>
...
</properties>
</persistence-unit>
</persistence>
No connection pooling
Propertites include connection information
Requires bean-managed transactions
@TransactionManagement(TransactionManagementType.BEAN)
Used for obscure situations - likely rarely used
<?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="ejbjpa-hotel-rl" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
...
<properties>
<!-- connection properties -->
<property name="javax.persistence.jdbc.url" value="${jdbc.url}"/>
<property name="javax.persistence.jdbc.driver" value="${jdbc.driver}"/>
<property name="javax.persistence.jdbc.user" value="${jdbc.user}"/>
<property name="javax.persistence.jdbc.password" value="${jdbc.password}"/>
<property name="hibernate.dialect" value="${hibernate.dialect}"/>
...
</properties>
</persistence-unit>
</persistence>
Use of transaction-type=RESOURCE_LOCAL bypasses a significant portion of what the application server provides (e.g., Connection Resource Pooling, Container Managed Transactions). This should only be used in limited cases and where the implementation requires. It should not be used because of lack of understanding of how to use the server-side JavaEE/EJB framework.
Deployment
META-INF/persistence.xml
Same location as normal JAR
Source
src/main/resources/META-INF/persistence.xml
enc-config-example-ejb |-- ejava | `-- ejb | `-- examples | `-- encconfig | |-- dto | | `-- NCPair.class | `-- ejb | |-- AnnotatedEJB.class | |-- InjectedEJB.class | |-- JNDIReader.class | |-- JNDIReaderRemote.class | `-- XMLConfiguredEJB.class `-- META-INF `-- persistence.xml
Deployment
WEB-INF/classes/META-INF/persistence.xml
Inside JARs within WEB-INF/lib also allowed
Source
src/main/resources/META-INF/persistence.xml
ejb-jpa-example-war |-- META-INF `-- WEB-INF |-- classes | |-- info | | `-- ejava | | `-- examples | | `-- ejb | | `-- ejbjpa | | |-- dto | | | |-- FloorDTO.class | | | `-- RoomDTO.class | | `-- ejb | | |-- HotelInitEJB.class | | |-- HotelInitRemote.class | | |-- HotelMgmtEJB.class | | |-- HotelMgmtLocal.class | | |-- HotelMgmtRemote.class | | |-- ReservationEJB.class | | `-- ReservationRemote.class | `-- META-INF | |-- ejb-jar.xml | |-- jboss-ejb3.xml | `-- persistence.xml `-- lib `-- ejb-jpa-example-blimpl-4.0.0-SNAPSHOT.jar
to have persistence.xml reference @Entity class(es) outside of local archive
jar-file element able to reference external JAR using a deterministic, portable path in EAR
ejbsessionBankEAR-4.0.0-SNAPSHOT |-- ejbsessionBankEJB.jar |-- ejbsessionBankWAR-4.0.0-SNAPSHOT.war |-- lib | |-- ejava-util-4.0.0-SNAPSHOT.jar | `-- ejbsessionBankImpl-4.0.0-SNAPSHOT.jar `-- META-INF `-- application.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="ejbsessionbank">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<jar-file>lib/ejbsessionBankImpl-4.0.0-SNAPSHOT.jar</jar-file>
<properties>
<property name="hibernate.dialect" value="${hibernate.dialect}"/>
...
</properties>
</persistence-unit>
</persistence>
Can automatically locate Entities within WEB-INF classes
Cannot automatically locate Entities within WEB-INF/lib JARs
jar-file element not usable in WARs
ejb-jpa-example-war |-- META-INF `-- WEB-INF ... `-- lib `-- ejb-jpa-example-blimpl-5.0.0-SNAPSHOT.jar
<?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="ejbjpa-hotel">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<!-- located in WEB-INF/lib/ejb-jpa-example-blimpl-${project.version}.jar -->
<class>info.ejava.examples.ejb.ejbjpa.bo.Guest</class>
<class>info.ejava.examples.ejb.ejbjpa.bo.Room</class>
<class>info.ejava.examples.ejb.ejbjpa.bo.Floor</class>
<properties>
<property name="hibernate.dialect" value="${hibernate.dialect}"/>
...
</properties>
</persistence-unit>
</persistence>
There is no need to declare "class" entity references into a properly configured "jar-file" that is located in the classpath of a deployed EAR. Use "class" entity references when using a WEB-INF/classes/META-INF deployment of a WAR and classes come from externally provided JAR -- or if you really need to only include specific entities in the archive.