Will an EntityManager with context type TRANSACTION in a Singleton join an external bean-managed transaction?
Suppose I have a Singleton
bean with an EntityManager
in it. The singleton also specifies (on method or class level) a transaction attribute REQUIRED
. The entity manager is obtained via an @PersistenceContext
injection which specifies persistence context type TRANSACTION
. For all intents and purposes, if a method on this singleton is invoked with an existing transaction, the entity manager should join the transaction or possibly provide an already existing one linked to that transaction via proxy. If such a method is invoked outside of a transaction a new transaction will be started for the duration of the method invocation.
Now suppose we have a second bean which uses bean-managed transactions and injects the singleton. If it explicitly starts a user transaction and then invokes a method on the singleton, will the entity manager in that method join that user transaction? Will the jump from bean-managed to container-managed transaction context even work? I know the other way around doesn't and forms a barrier.
The singleton class:
@Singleton
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class PersistenceSingleton {
@PersistenceContext(unitName = "test", type = PersistenceContextType.TRANSACTION)
private EntityManager em;
public void doStuff() {
// perform actions with the entity manager that imply changes in the database
}
}
The bean with user transactions (might as well be stateless or stateful):
@Singleton
@TransactionManagement(TransactionManagementType.BEAN)
public class PersistenceFacade {
@EJB
private PeristenceSingleton ps;
@Resource
private UserTransaction userTx;
public void doStuff() {
userTx.begin();
ps.doStuff();
userTx.commit();
}
}
Does the transaction started in method doStuff()
of the PersistenceFacade
get taken into account when invoking doStuff()
on the PersistenceSingleton
? Does the entity manager automatically join the transaction and behave as expected from transaction isolation during concurrent access?
java jpa java-ee transactions entitymanager
add a comment |
Suppose I have a Singleton
bean with an EntityManager
in it. The singleton also specifies (on method or class level) a transaction attribute REQUIRED
. The entity manager is obtained via an @PersistenceContext
injection which specifies persistence context type TRANSACTION
. For all intents and purposes, if a method on this singleton is invoked with an existing transaction, the entity manager should join the transaction or possibly provide an already existing one linked to that transaction via proxy. If such a method is invoked outside of a transaction a new transaction will be started for the duration of the method invocation.
Now suppose we have a second bean which uses bean-managed transactions and injects the singleton. If it explicitly starts a user transaction and then invokes a method on the singleton, will the entity manager in that method join that user transaction? Will the jump from bean-managed to container-managed transaction context even work? I know the other way around doesn't and forms a barrier.
The singleton class:
@Singleton
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class PersistenceSingleton {
@PersistenceContext(unitName = "test", type = PersistenceContextType.TRANSACTION)
private EntityManager em;
public void doStuff() {
// perform actions with the entity manager that imply changes in the database
}
}
The bean with user transactions (might as well be stateless or stateful):
@Singleton
@TransactionManagement(TransactionManagementType.BEAN)
public class PersistenceFacade {
@EJB
private PeristenceSingleton ps;
@Resource
private UserTransaction userTx;
public void doStuff() {
userTx.begin();
ps.doStuff();
userTx.commit();
}
}
Does the transaction started in method doStuff()
of the PersistenceFacade
get taken into account when invoking doStuff()
on the PersistenceSingleton
? Does the entity manager automatically join the transaction and behave as expected from transaction isolation during concurrent access?
java jpa java-ee transactions entitymanager
add a comment |
Suppose I have a Singleton
bean with an EntityManager
in it. The singleton also specifies (on method or class level) a transaction attribute REQUIRED
. The entity manager is obtained via an @PersistenceContext
injection which specifies persistence context type TRANSACTION
. For all intents and purposes, if a method on this singleton is invoked with an existing transaction, the entity manager should join the transaction or possibly provide an already existing one linked to that transaction via proxy. If such a method is invoked outside of a transaction a new transaction will be started for the duration of the method invocation.
Now suppose we have a second bean which uses bean-managed transactions and injects the singleton. If it explicitly starts a user transaction and then invokes a method on the singleton, will the entity manager in that method join that user transaction? Will the jump from bean-managed to container-managed transaction context even work? I know the other way around doesn't and forms a barrier.
The singleton class:
@Singleton
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class PersistenceSingleton {
@PersistenceContext(unitName = "test", type = PersistenceContextType.TRANSACTION)
private EntityManager em;
public void doStuff() {
// perform actions with the entity manager that imply changes in the database
}
}
The bean with user transactions (might as well be stateless or stateful):
@Singleton
@TransactionManagement(TransactionManagementType.BEAN)
public class PersistenceFacade {
@EJB
private PeristenceSingleton ps;
@Resource
private UserTransaction userTx;
public void doStuff() {
userTx.begin();
ps.doStuff();
userTx.commit();
}
}
Does the transaction started in method doStuff()
of the PersistenceFacade
get taken into account when invoking doStuff()
on the PersistenceSingleton
? Does the entity manager automatically join the transaction and behave as expected from transaction isolation during concurrent access?
java jpa java-ee transactions entitymanager
Suppose I have a Singleton
bean with an EntityManager
in it. The singleton also specifies (on method or class level) a transaction attribute REQUIRED
. The entity manager is obtained via an @PersistenceContext
injection which specifies persistence context type TRANSACTION
. For all intents and purposes, if a method on this singleton is invoked with an existing transaction, the entity manager should join the transaction or possibly provide an already existing one linked to that transaction via proxy. If such a method is invoked outside of a transaction a new transaction will be started for the duration of the method invocation.
Now suppose we have a second bean which uses bean-managed transactions and injects the singleton. If it explicitly starts a user transaction and then invokes a method on the singleton, will the entity manager in that method join that user transaction? Will the jump from bean-managed to container-managed transaction context even work? I know the other way around doesn't and forms a barrier.
The singleton class:
@Singleton
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class PersistenceSingleton {
@PersistenceContext(unitName = "test", type = PersistenceContextType.TRANSACTION)
private EntityManager em;
public void doStuff() {
// perform actions with the entity manager that imply changes in the database
}
}
The bean with user transactions (might as well be stateless or stateful):
@Singleton
@TransactionManagement(TransactionManagementType.BEAN)
public class PersistenceFacade {
@EJB
private PeristenceSingleton ps;
@Resource
private UserTransaction userTx;
public void doStuff() {
userTx.begin();
ps.doStuff();
userTx.commit();
}
}
Does the transaction started in method doStuff()
of the PersistenceFacade
get taken into account when invoking doStuff()
on the PersistenceSingleton
? Does the entity manager automatically join the transaction and behave as expected from transaction isolation during concurrent access?
java jpa java-ee transactions entitymanager
java jpa java-ee transactions entitymanager
asked Nov 14 '18 at 14:19
G_HG_H
9,80022265
9,80022265
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
UserTransaction is used for changing the default transaction demarcation but we still control the JTA transactions.
https://www.javacodegeeks.com/2013/03/types-of-entity-managers-application-managed-entitymanager.html says:
If you have UserTransaction you can start demarcating what is to be executed within the transaction. Notice that you’re still controlling
the JTA transactions
so The persistence context propagation rule will be applied for UserTransaction demarcation.
pro JPA book says:
When a method is invoked on the transaction-scoped entity manager, it
must first see whether there is a propagated persistence context. If
one exists, the entity manager uses this persistence context to carry
out the operation. All subsequent transaction-scoped entity manager
operations, in this component or any other, will thereafter use this
newly created persistence context. This behavior works independently
of whether container-managed or bean-managed transaction demarcation
has been used.
the answer to your questions is yes(first question)
Does the entity manager automatically join the transaction and behave
as expected from transaction isolation during concurrent access?
entity manager checks the existence of a propagated persistence context and uses it.
1
Perfect, thank you! I left this out in the example code but I actually specify bean-managed concurrency on the "facade" class (the one using the singleton with the entity manager) and will either do the same or use read locks on the singleton. Although the entity manager itself may not be thread-safe, since the invocations from different threads will have separate transactions it should be using separate persistence contexts anyway. I'll dig some more into whether that avoids any issues or not.
– G_H
Nov 15 '18 at 9:02
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302359%2fwill-an-entitymanager-with-context-type-transaction-in-a-singleton-join-an-exter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
UserTransaction is used for changing the default transaction demarcation but we still control the JTA transactions.
https://www.javacodegeeks.com/2013/03/types-of-entity-managers-application-managed-entitymanager.html says:
If you have UserTransaction you can start demarcating what is to be executed within the transaction. Notice that you’re still controlling
the JTA transactions
so The persistence context propagation rule will be applied for UserTransaction demarcation.
pro JPA book says:
When a method is invoked on the transaction-scoped entity manager, it
must first see whether there is a propagated persistence context. If
one exists, the entity manager uses this persistence context to carry
out the operation. All subsequent transaction-scoped entity manager
operations, in this component or any other, will thereafter use this
newly created persistence context. This behavior works independently
of whether container-managed or bean-managed transaction demarcation
has been used.
the answer to your questions is yes(first question)
Does the entity manager automatically join the transaction and behave
as expected from transaction isolation during concurrent access?
entity manager checks the existence of a propagated persistence context and uses it.
1
Perfect, thank you! I left this out in the example code but I actually specify bean-managed concurrency on the "facade" class (the one using the singleton with the entity manager) and will either do the same or use read locks on the singleton. Although the entity manager itself may not be thread-safe, since the invocations from different threads will have separate transactions it should be using separate persistence contexts anyway. I'll dig some more into whether that avoids any issues or not.
– G_H
Nov 15 '18 at 9:02
add a comment |
UserTransaction is used for changing the default transaction demarcation but we still control the JTA transactions.
https://www.javacodegeeks.com/2013/03/types-of-entity-managers-application-managed-entitymanager.html says:
If you have UserTransaction you can start demarcating what is to be executed within the transaction. Notice that you’re still controlling
the JTA transactions
so The persistence context propagation rule will be applied for UserTransaction demarcation.
pro JPA book says:
When a method is invoked on the transaction-scoped entity manager, it
must first see whether there is a propagated persistence context. If
one exists, the entity manager uses this persistence context to carry
out the operation. All subsequent transaction-scoped entity manager
operations, in this component or any other, will thereafter use this
newly created persistence context. This behavior works independently
of whether container-managed or bean-managed transaction demarcation
has been used.
the answer to your questions is yes(first question)
Does the entity manager automatically join the transaction and behave
as expected from transaction isolation during concurrent access?
entity manager checks the existence of a propagated persistence context and uses it.
1
Perfect, thank you! I left this out in the example code but I actually specify bean-managed concurrency on the "facade" class (the one using the singleton with the entity manager) and will either do the same or use read locks on the singleton. Although the entity manager itself may not be thread-safe, since the invocations from different threads will have separate transactions it should be using separate persistence contexts anyway. I'll dig some more into whether that avoids any issues or not.
– G_H
Nov 15 '18 at 9:02
add a comment |
UserTransaction is used for changing the default transaction demarcation but we still control the JTA transactions.
https://www.javacodegeeks.com/2013/03/types-of-entity-managers-application-managed-entitymanager.html says:
If you have UserTransaction you can start demarcating what is to be executed within the transaction. Notice that you’re still controlling
the JTA transactions
so The persistence context propagation rule will be applied for UserTransaction demarcation.
pro JPA book says:
When a method is invoked on the transaction-scoped entity manager, it
must first see whether there is a propagated persistence context. If
one exists, the entity manager uses this persistence context to carry
out the operation. All subsequent transaction-scoped entity manager
operations, in this component or any other, will thereafter use this
newly created persistence context. This behavior works independently
of whether container-managed or bean-managed transaction demarcation
has been used.
the answer to your questions is yes(first question)
Does the entity manager automatically join the transaction and behave
as expected from transaction isolation during concurrent access?
entity manager checks the existence of a propagated persistence context and uses it.
UserTransaction is used for changing the default transaction demarcation but we still control the JTA transactions.
https://www.javacodegeeks.com/2013/03/types-of-entity-managers-application-managed-entitymanager.html says:
If you have UserTransaction you can start demarcating what is to be executed within the transaction. Notice that you’re still controlling
the JTA transactions
so The persistence context propagation rule will be applied for UserTransaction demarcation.
pro JPA book says:
When a method is invoked on the transaction-scoped entity manager, it
must first see whether there is a propagated persistence context. If
one exists, the entity manager uses this persistence context to carry
out the operation. All subsequent transaction-scoped entity manager
operations, in this component or any other, will thereafter use this
newly created persistence context. This behavior works independently
of whether container-managed or bean-managed transaction demarcation
has been used.
the answer to your questions is yes(first question)
Does the entity manager automatically join the transaction and behave
as expected from transaction isolation during concurrent access?
entity manager checks the existence of a propagated persistence context and uses it.
edited Nov 28 '18 at 11:48
answered Nov 14 '18 at 19:29
Mehran MastcheshmiMehran Mastcheshmi
4081310
4081310
1
Perfect, thank you! I left this out in the example code but I actually specify bean-managed concurrency on the "facade" class (the one using the singleton with the entity manager) and will either do the same or use read locks on the singleton. Although the entity manager itself may not be thread-safe, since the invocations from different threads will have separate transactions it should be using separate persistence contexts anyway. I'll dig some more into whether that avoids any issues or not.
– G_H
Nov 15 '18 at 9:02
add a comment |
1
Perfect, thank you! I left this out in the example code but I actually specify bean-managed concurrency on the "facade" class (the one using the singleton with the entity manager) and will either do the same or use read locks on the singleton. Although the entity manager itself may not be thread-safe, since the invocations from different threads will have separate transactions it should be using separate persistence contexts anyway. I'll dig some more into whether that avoids any issues or not.
– G_H
Nov 15 '18 at 9:02
1
1
Perfect, thank you! I left this out in the example code but I actually specify bean-managed concurrency on the "facade" class (the one using the singleton with the entity manager) and will either do the same or use read locks on the singleton. Although the entity manager itself may not be thread-safe, since the invocations from different threads will have separate transactions it should be using separate persistence contexts anyway. I'll dig some more into whether that avoids any issues or not.
– G_H
Nov 15 '18 at 9:02
Perfect, thank you! I left this out in the example code but I actually specify bean-managed concurrency on the "facade" class (the one using the singleton with the entity manager) and will either do the same or use read locks on the singleton. Although the entity manager itself may not be thread-safe, since the invocations from different threads will have separate transactions it should be using separate persistence contexts anyway. I'll dig some more into whether that avoids any issues or not.
– G_H
Nov 15 '18 at 9:02
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302359%2fwill-an-entitymanager-with-context-type-transaction-in-a-singleton-join-an-exter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown