The @Transactional annotation uses aop method interception (see JpaLocalTxnInterceptor).
This means the annotation works only when all of the following is true:
- The concrete instance was created by Guice and not by anybody else (other framework or calling new)
- the annotated method is not private
- the annotated method is not final
- the annotated method is not static
In your case I would suspect that maybe the instance was created by Jersey (and Jersey then passes the instance to Guice for member injection) and not by Guice itself.
If this doesn't help you may need to post some code or reduce the problem to a minimal setup for further exploration.
regards
Stephan
On 11/21/2013 12:35 AM, Armin Bahramshahry wrote:
Hi,
I've setup Guice with Jersey, and the PersistFilter. Trying to use @Transactional annotation, and it seems to be working if the object is binded in the Singleton scope, or Request scope (from within the servlet module binding); however, objects that are just binded with the basic binding (none) they seem to have problem with @Transactional.
Getting the EntityManager seem to work fine, etc, but nothing gets committed.
Simply moving the binding of the object from the AbstractModule to ServletModule makes the @Transactional to work as expected. The @Transactional seem to also work for all objects that are marked as Singleton.
Was wondering if this is an intended behavior or not, and if not, what could be causing this?
Regards,
Armin
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+unsubscribe@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+unsubscribe@googlegroups.com.
With Guice/Jersey, we've always had to inject a proxy/delegate class that contains all our AOP annotated methods. As Stephan said, Jersey resource classes are already created/instrumented and Guice then no longer has the opportunity to wrap it with its AOP functionality.
On Thu, Nov 21, 2013 at 1:52 AM, Stephan Classen <st.cl...@gmx.ch> wrote:
The @Transactional annotation uses aop method interception (see JpaLocalTxnInterceptor).
This means the annotation works only when all of the following is true:
- The concrete instance was created by Guice and not by anybody else (other framework or calling new)
- the annotated method is not private
- the annotated method is not final
- the annotated method is not static
In your case I would suspect that maybe the instance was created by Jersey (and Jersey then passes the instance to Guice for member injection) and not by Guice itself.
If this doesn't help you may need to post some code or reduce the problem to a minimal setup for further exploration.
regards
Stephan
On 11/21/2013 12:35 AM, Armin Bahramshahry wrote:
Hi,
I've setup Guice with Jersey, and the PersistFilter. Trying to use @Transactional annotation, and it seems to be working if the object is binded in the Singleton scope, or Request scope (from within the servlet module binding); however, objects that are just binded with the basic binding (none) they seem to have problem with @Transactional.
Getting the EntityManager seem to work fine, etc, but nothing gets committed.
Simply moving the binding of the object from the AbstractModule to ServletModule makes the @Transactional to work as expected. The @Transactional seem to also work for all objects that are marked as Singleton.
Was wondering if this is an intended behavior or not, and if not, what could be causing this?
Regards,
Armin
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
It's something like this right now.public class Module extends AbstractModule {@Overrideprotected void configure() {install(new ServletModule());}}public class ServletModule {@Overrideprotected void configureServlets() {install(new FactoryModuleBuilder().implement(H.class, X.class, XI.class).implement(H.class, A.class, AI.class).build(HFactory.class);//... other bindings}Originally, the "install(new Factory...") section was in Module class (above "install(new ServletModule())").Definitions:public abstract class H extends Callable<Void> {//...@Transactional@Overridepublic void call() {//...}}public class XI extends Hpublic class AI extends H@BindingAnnotation @Target({ METHOD }) @Retention(RUNTIME)public @interface X@BindingAnnotation @Target({ METHOD }) @Retention(RUNTIME)public @interface A
--
Although this thread is old, I just happened to come across while trying to understands internals of guice @Transactional.If class A has caller method that calls a public @Transactional method in it's own class, does Transactional really apply?If class A has dependency injected of class B(Singleton), and if class A calls a public @Transactional method of class B, I believe Transaction is definitely applied. However, it seems that for above scenario(calling method of it's own class), I believe Transaction should not be applied. Could you please let me know if I am correct?
@Override
public void foo() {
try {
// …begin transaction…
super.foo();
} catch (Exception e) {
if (shouldRollback(e)) { // depends on @Transactional(rollbackOn=)
// …rollback…
} else {
// …commit…
}
throw e;
}
// …commit…
}
class UseCase {
public Result execute() {
// preconditions
try {
doInTransaction();
return Result.SUCCESS;
} catch (NoDataFoundException e) {
// …log…
return Result.NOT_FOUND;
} catch (Exception e) {
// …log…
return Result.ERROR
}
}
@Transactional
void doInTransaction() {
// …
}
}doInTransaction();
// …
@Transactional void doInTransaction() { … }transactionManager.transaction(this::doInTransaction);
// …
private void doInTransaction() { … }