-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5efcbfd
commit f40a0bf
Showing
8 changed files
with
89 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/com/piotrnowicki/emf/boundary/BeanABoundary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters