guice - @Transactional - not working

1.1k views Asked by At

I am using guice 4.1 along with the persist extension and hoping that @Transactional will work, but it isn't. I followed the wiki and read this post, but to no avail:

Managing transactions in JavaSE with Hibernate and Guice

Just to re-iterate ... I am doing:

  1. depending on guice-persist
  2. using google's @Transactional annotation
  3. using the annotation only on public or protected methods that are managed by guice
  4. starting the PersistService ...
1

There are 1 answers

1
WarrenB SA On BEST ANSWER

We ran into something similar when upgrading our libraries in our current Sprint. All our failing unit tests were related to classes that we're being injected, but were not bound in the module.

(Which wasn't a problem using Guice 4.0).

We tend to only bind our classes if we bind to an interface or some special requirement. So if we had a simple class that had a zero argument constructor or was annotated with @Inject, we wouldn't bind it in the module.

public class AA {

    public AA() {

    }

    @Transactional
    public void test() {
        System.out.println("" + getClass().getName());
    }
}

If the class AA was injected, calling test() would output

package.test.AA$$EnhancerByGuice$$e2d19b62

After upgrading to Guice 4.1 the output is

package.test.AA

Adding the following to the module changes the injection back to a proxy. Which is how the @Transactional works using AOP

bind(AA.class);