Skip to content

Commit

Permalink
added CDI injectione xample
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrNowicki committed Nov 18, 2012
1 parent 5efcbfd commit f40a0bf
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 6 deletions.
8 changes: 4 additions & 4 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="owner.project.facets" value="java"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
Expand Down
2 changes: 1 addition & 1 deletion .settings/org.eclipse.wst.common.component
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<property name="java-output-path" value="/emf-jta/target/classes"/>
<property name="context-root" value="ejb-interceptors-spike"/>
<property name="context-root" value="emf-jta"/>
</wb-module>
</project-modules>
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<url>http://www.piotrnowicki.com</url>
<scm>
<url>https://github.com/PiotrNowicki/emf-and-jta</url>
</scm>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/piotrnowicki/emf/boundary/BeanABoundary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.piotrnowicki.emf.boundary;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.inject.Inject;
import javax.persistence.EntityManager;

@Stateless
@TransactionAttribute(TransactionAttributeType.NEVER)
public class BeanABoundary {

@Inject
private EntityManager em;

@EJB
BeanB beanB;

public void invoke() {

/**
* This operation makes CDI to inject (create) EntityManager. As this bean is non-transaction, the EntityManager won't
* be transactional as well.
*
* Because the CDI implementor can lazy-load the injected resource - without invoking any operation on the resource it
* won't be injected. So the bottom line is: without this invocation (or any other on "em") we will be safe as the
* EntityManager won't be created in non-transactional bean.
*
* However, if this operation is uncommented, we won't get any exceptions but we will work on non-transactional EntityManager for the whole request.
*/
em.getProperties();

beanB.invoke();
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/piotrnowicki/emf/boundary/BeanB.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.piotrnowicki.emf.boundary;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;

import com.piotrnowicki.emf.entity.Customer;

@Stateless
public class BeanB {

// This is the same EntityManager as in BeanABoundary thanks to the CDI Request Scoped.
@Inject
private EntityManager em;

public void invoke() {
em.persist(new Customer("CDITest1", "CDITest2"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.transaction.UserTransaction;

Expand Down Expand Up @@ -38,6 +39,9 @@ public class CustomerBoundary {

@EJB
private CustomerControl beanB;

@PersistenceContext
EntityManager em;

/**
* We're injecting the JTA Resource (take a look at {@code persistence.xml} - there is only one, default, PersistenceUnit
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/piotrnowicki/emf/web/Entities.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import javax.ws.rs.GET;
import javax.ws.rs.Path;

import com.piotrnowicki.emf.boundary.BeanABoundary;
import com.piotrnowicki.emf.boundary.CustomerBoundary;
import com.piotrnowicki.emf.entity.Customer;

Expand All @@ -23,6 +24,9 @@ public class Entities {
@EJB
private CustomerBoundary customerBoundary;

@EJB
private BeanABoundary beanABoundary;

/*
* This omits the CDI Producer method as we're using @PersistceContext and not @Inject
*/
Expand Down Expand Up @@ -50,6 +54,21 @@ public String add() throws Exception {
return getAll();
}

/**
* Invoke this method if you want to see the results of CDI RequestScoped injected application-managed EntityManager. If the
* bean was properly executed we should return the new customer named 'CDITest1'.
*
* @return
* @throws Exception
*/
@Path("cdi")
@GET
public String injectUsingCdi() throws Exception {
beanABoundary.invoke();

return getAll();
}

private String getAll() {
List<Customer> customers = em.createNamedQuery(Customer.FIND_ALL, Customer.class).getResultList();

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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="emfPool">
<persistence-unit name="emfPool" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create" />
Expand Down

0 comments on commit f40a0bf

Please sign in to comment.