You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by gp...@apache.org on 2012/12/15 21:12:31 UTC
svn commit: r1422340 -
/incubator/deltaspike/site/trunk/content/deltaspike/jpa.mdtext
Author: gpetracek
Date: Sat Dec 15 20:12:30 2012
New Revision: 1422340
URL: http://svn.apache.org/viewvc?rev=1422340&view=rev
Log:
updated content
Modified:
incubator/deltaspike/site/trunk/content/deltaspike/jpa.mdtext
Modified: incubator/deltaspike/site/trunk/content/deltaspike/jpa.mdtext
URL: http://svn.apache.org/viewvc/incubator/deltaspike/site/trunk/content/deltaspike/jpa.mdtext?rev=1422340&r1=1422339&r2=1422340&view=diff
==============================================================================
--- incubator/deltaspike/site/trunk/content/deltaspike/jpa.mdtext (original)
+++ incubator/deltaspike/site/trunk/content/deltaspike/jpa.mdtext Sat Dec 15 20:12:30 2012
@@ -16,30 +16,31 @@ Notice: Licensed to the Apache Softwa
specific language governing permissions and limitations
under the License.
-## @Transactional
+# @Transactional
This annotation is an alternative to transactional EJBs which allows to execute a method within a transaction.
-Before it's possible to start using the annotation, it's required to implement a CDI producer for an {{EntityManager}} and it's needed to inject the {{EntityManager}} in the bean which uses {{@Transactional}}. As shown later on it's also possible to use multiple qualifiers for using different {{EntityManager}}s.
+Before it's possible to start using the annotation, it's required to implement a CDI producer for an `EntityManager` and it's needed to inject the `EntityManager` in the bean which uses `@Transactional`. As shown later on it's also possible to use multiple qualifiers for using different `EntityManager`s.
-The following example shows a simple producer for an {{EntityManager}} and the corresponding dispose-method.
+The following example shows a simple producer for an `EntityManager` and the corresponding dispose-method.
Producing it as request scoped bean means that the dispose method will be called on finishing the request.
-As an alternative it's possible to use a special scope called {{@TransactionScoped}} provided by the same DeltaSpike module.
+As an alternative it's possible to use a special scope called `@TransactionScoped` provided by the same DeltaSpike module.
+
+Producer for the default EntityManager:
- Producer for the default EntityManager
//...
public class EntityManagerProducer
{
//or manual bootstrapping
@PersistenceContext
private EntityManager entityManager;
-
+
@Produces
@RequestScoped
protected EntityManager createEntityManager()
{
return this.entityManager;
}
-
+
protected void closeEntityManager(@Disposes EntityManager entityManager)
{
if (entityManager.isOpen())
@@ -49,15 +50,16 @@ As an alternative it's possible to use a
}
}
-The following examples show how to use the {{EntityManager}} produced by the example above.
+The following examples show how to use the `EntityManager` produced by the example above.
+
+Beans with transactional method:
- Beans with transactional method
//...
public class TransactionalBean
{
@Inject
private EntityManager entityManager;
-
+
@Transactional
public void executeInTransaction()
{
@@ -65,51 +67,54 @@ The following examples show how to use t
}
}
- Simple transactional bean (all methods transactional)
+Simple transactional bean (all methods transactional):
+
//...
@Transactional
public class TransactionalBean
{
@Inject
private EntityManager entityManager;
-
+
//...
}
-As illustrated in the following example it's also possible to use {{@Transactional}} for stereotypes.
+As illustrated in the following example it's also possible to use `@Transactional` for stereotypes.
+
+Stereotype for transactional beans (+ usage):
- Stereotype for transactional beans (+ usage)
@Stereotype
@Transactional
@ApplicationScoped
public @interface Repository
{
}
-
+
//...
@Repository
public class TransactionalBean
{
@Inject
private EntityManager entityManager;
-
+
//...
}
Besides such simple usages, it's also supported to use qualifiers to access multiple persistence-units in parallel.
-The default qualifier for {{@Transactional}} is {{@Any}}. Therefore a transaction for every injected entity manager will be started.
+The default qualifier for `@Transactional` is `@Any`. Therefore a transaction for every injected entity manager will be started.
The example afterwards shows how to change this default behaviour.
- Producer for multiple entity managers (+ usage)
+Producer for multiple entity managers (+ usage):
+
//...
public class EntityManagerProducer
{
@PersistenceContext(unitName = "firstDB")
private EntityManager firstEntityManager;
-
+
@PersistenceContext(unitName = "secondDB")
private EntityManager secondEntityManager;
-
+
@Produces
@First
@RequestScoped
@@ -117,15 +122,15 @@ The example afterwards shows how to chan
{
return this.firstEntityManager;
}
-
+
protected void closeFirstEntityManager(@Disposes @First EntityManager entityManager)
{
- if (firstEntityManager.isOpen())
+ if (entityManager.isOpen())
{
- firstEntityManager.close();
+ entityManager.close();
}
}
-
+
@Produces
@Second
@RequestScoped
@@ -133,26 +138,25 @@ The example afterwards shows how to chan
{
return this.secondEntityManager;
}
-
+
protected void closeSecondEntityManager(@Disposes @Second EntityManager entityManager)
{
- if (secondEntityManager.isOpen())
+ if (entityManager.isOpen())
{
- secondEntityManager.close();
+ entityManager.close();
}
}
}
-
-
+
//...
public class FirstLevelTransactionBean
{
@Inject
private @First EntityManager firstEntityManager;
-
+
@Inject
private NestedTransactionBean nestedTransactionBean;
-
+
@Transactional
public void executeInTransaction()
{
@@ -160,13 +164,13 @@ The example afterwards shows how to chan
this.nestedTransactionBean.executeInTransaction();
}
}
-
+
//...
public class NestedTransactionBean
{
@Inject
private @Second EntityManager secondEntityManager;
-
+
@Transactional
public void executeInTransaction()
{
@@ -174,35 +178,36 @@ The example afterwards shows how to chan
}
}
-The following example shows how to use only the specified {{EntityManager}}/s
+The following example shows how to use only the specified `EntityManager`/s
+
+Activating entity managers manually:
- Activating entity managers manually
public class MultiTransactionBean
{
@Inject
private EntityManager defaultEntityManager;
-
+
@Inject
private @First EntityManager firstEntityManager;
-
+
@Inject
private @Second EntityManager secondEntityManager;
-
+
@Transactional(qualifier = Default.class)
public void executeInDefaultTransaction()
{
}
-
+
@Transactional(qualifier = First.class)
public void executeInFirstTransaction()
{
}
-
+
@Transactional(qualifier = Second.class)
public void executeInSecondTransaction()
{
}
-
+
@Transactional(qualifier = {First.class, Second.class})
public void executeInFirstAndSecondTransaction()
{
@@ -211,29 +216,30 @@ The following example shows how to use o
All examples also work with nested calls. In the following example the transaction handling is done on the entry point (after FirstLevelTransactionBean#executeInTransaction).
- Joining existing transaction in nested call
+Joining existing transaction in nested call:
+
//...
public class FirstLevelTransactionBean
{
@Inject
private EntityManager entityManager;
-
+
@Inject
private NestedTransactionBean nestedTransactionBean;
-
+
@Transactional
public void executeInTransaction()
{
this.nestedTransactionBean.executeInTransaction();
}
}
-
+
//...
public class NestedTransactionBean
{
@Inject
private EntityManager entityManager;
-
+
@Transactional
public void executeInTransaction()
{
@@ -241,28 +247,29 @@ All examples also work with nested calls
}
}
-The final transaction handling for all {{EntityManager}} s is also done after the outermost transactional method if {{NestedTransactionBean}} uses a different {{EntityManager}}.
-So it's possible to catch an exception in {{FirstLevelTransactionBean}} e.g. to try an optional path instead of an immediate rollback.
+The final transaction handling for all `EntityManager` s is also done after the outermost transactional method if `NestedTransactionBean` uses a different `EntityManager`.
+So it's possible to catch an exception in `FirstLevelTransactionBean` e.g. to try an optional path instead of an immediate rollback.
+
+# @TransactionScoped
-## @TransactionScoped
+`@Transactional` also starts a context which is available as long as the transaction started by `@Transactional`. Besides other beans you can use this scope for the `EntityManager` itself. That means the `EntityManager` will be closed after leaving the method annotated with `@Transactional`.
-{{@Transactional}} also starts a context which is available as long as the transaction started by {{@Transactional}}. Besides other beans you can use this scope for the {{EntityManager}} itself. That means the {{EntityManager}} will be closed after leaving the method annotated with {{@Transactional}}.
+Producer for the default EntityManager which should be used only for one transaction:
- Producer for the default EntityManager which should be used only for one transaction
//...
public class EntityManagerProducer
{
//or manual bootstrapping
@PersistenceContext
private EntityManager entityManager;
-
+
@Produces
@TransactionScoped
protected EntityManager createEntityManager()
{
return this.entityManager;
}
-
+
protected void closeEntityManager(@Disposes EntityManager entityManager)
{
if (entityManager.isOpen())
@@ -270,4 +277,4 @@ So it's possible to catch an exception i
entityManager.close();
}
}
- }
+ }
\ No newline at end of file