The code that instantiates the TestEndpoint according to me is the 
ServletModule that is created in the GuiceContextListener and looks like 
this:
public class ServletModule extends com.google.inject.servlet.ServletModule {
    private static final String JERSEY_SCAN_PACKAGE = 
"eu.vranckaert.test.json.endpoint.impl";
    private static final String JERSEY_POJO_MAPPING_FEATURE = "true";

    @Override
    protected void configureServlets() {
        /*
 * The following line will scan our package for Jersey Resources
 */

        final Map<String, String> params = new HashMap<String, String>();
        params.put("com.sun.jersey.config.property.packages", 
JERSEY_SCAN_PACKAGE);
        params.put("com.sun.jersey.api.json.POJOMappingFeature", 
JERSEY_POJO_MAPPING_FEATURE);

        filter("/*").through(PersistFilter.class);
        serve("/rest/*").with(GuiceContainer.class, params);
    }
}

And this is all there is to the TestEndpoint class:

package eu.vranckaert.test.json.endpoint.impl;

import com.google.inject.persist.Transactional;
import eu.vranckaert.test.model.User;
import eu.vranckaert.test.security.dao.UserDao;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.persistence.EntityManager;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * Date: 09/09/13
 * Time: 08:19
 *
 * @author Dirk Vranckaert
 */
@Path("test")
public class TestEndpoint {
    @Inject
    private UserDao userDao;

    @Inject
    private Provider<EntityManager> em;

    @GET
    @Path("persistUser")
    @Produces(MediaType.TEXT_PLAIN)
    @Transactional
    public String persistUser() {
        User user = new User();
        user.setEmail("[email protected]");
        user.setFirstName("FirstName");
        user.setLastName("LastName");
        user = userDao.persist(user);
        return "ok";
    }
}


Op vrijdag 18 oktober 2013 11:40:46 UTC+2 schreef scl:
>
>  I think the problem is that the instance is not created by guice.
> as Thomas mentioned the @Transactional only works on instances created by 
> guice.
>
> From you callstack it looks like the instance could be created by jersey.
> To clarify this could you paste the code of TestEndpoint.class
>
> Thanks
>
>
> On 10/18/2013 11:35 AM, Dirk Vranckaert wrote:
>  
> And here is the callstack: http://pastebin.com/raw.php?i=d6QVTA6B 
> :-)
>
> Op vrijdag 18 oktober 2013 11:29:15 UTC+2 schreef Dirk Vranckaert: 
>>
>> Here is my entire callstack of all the threads, the second thread 
>> (indicating running) is the one I'm working on. In the entire callstack I 
>> don't see anything related to JpaLocalTxnInterceptor. 
>> So my config is wrong at some point?
>>
>> Op vrijdag 18 oktober 2013 11:18:13 UTC+2 schreef scl: 
>>>
>>>  could you place a break point inside the persistUser() method and check 
>>> the callstack to see if there is a JpaLocalTxnInterceptor.
>>> This class is responsible for handling the @Transactional annotation.
>>>
>>>
>>> On 10/18/2013 11:06 AM, Dirk Vranckaert wrote:
>>>  
>>> I'm building a backend application for one of my Android applications. 
>>> My setup is Hibernate as ORM, MySQL as DB, Guice as DI and Guice-Persist 
>>> for the link between ORM and DI.
>>>
>>>  My webservice-endpoint class looks like this:
>>>  
>>>  @Path("test")
>>> public class TestEndpoint {
>>>     @Inject
>>>     private UserDao userDao;
>>>
>>>      @GET
>>>     @Path("persistUser")
>>>     @Produces(MediaType.TEXT_PLAIN)
>>>     @Transactional
>>>     public String persistUser() {
>>>         User user = new User();
>>>         user.setEmail("[email protected]");
>>>         user.setFirstName("FirstName");
>>>         user.setLastName("LastName");
>>>         user = userDao.persist(user);
>>>         return "ok";
>>>     }
>>> }
>>>  
>>>  When I manually manage the transaction it works and my entity is 
>>> persisted:
>>>
>>>  @Path("test")
>>> public class TestEndpoint {
>>>     @Inject
>>>     private UserDao userDao;
>>>
>>>      @Inject
>>>     private Provider<EntityManager> em;
>>>
>>>      @GET
>>>     @Path("persistUser")
>>>     @Produces(MediaType.TEXT_PLAIN)
>>>     public String persistUser() {
>>>          
>>> ((HibernateEntityManager)em.get()).getSession().getTransaction().begin();
>>>  
>>>         User user = new User();
>>>         user.setEmail("[email protected]");
>>>         user.setFirstName("FirstName");
>>>         user.setLastName("LastName");
>>>         user = userDao.persist(user);
>>>  
>>>          
>>> ((HibernateEntityManager)em.get()).getSession().getTransaction().commit();
>>>  
>>>          return "ok";
>>>     }
>>> }
>>>  
>>>  My UseDaoImpl that is injected via UserDao looks like this:
>>>
>>>  public abstract class UserDaoImpl {
>>>      @Inject
>>>     private Provider<EntityManager> em;
>>>
>>>      public EntityManager getEntityManager() {
>>>         return em.get();
>>>     }
>>>      public User persist(User instance) {
>>>         getEntityManager().persist(instance);
>>>          return instance;
>>>     }
>>>
>>>      public User findById(String id) {
>>>         return getEntityManager().find(User.class, id);
>>>     }
>>> }
>>>  
>>>  My GuiceContextListener looks like this:
>>>  public class GuiceContextListener extends GuiceServletContextListener {
>>>     @Override
>>>     protected Injector getInjector() {
>>>         Injector injector = Guice.createInjector(
>>>                 new DatabaseModule(),
>>>                 new GuiceDaoModule(),
>>>                 new GuiceServiceModule(),
>>>                 new ServletModule()
>>>         );
>>>         return injector;
>>>     }
>>> }
>>>  
>>>  The DatabaseModule:
>>>  public class DatabaseModule extends AbstractModule {
>>>     private static final String JPA_UNIT = "myDB";
>>>
>>>      @Override
>>>     protected void configure() {
>>>         install(new JpaPersistModule(JPA_UNIT));
>>>     }
>>> }
>>>  
>>>  The GuiceDaoModule:
>>>  public class GuiceDaoModule implements Module {
>>>     @Override
>>>     public void configure(Binder binder) {
>>>         binder.bind(UserDao.class).to(UserDaoImpl.class);
>>>      }
>>> }
>>>  
>>>  And this is my web.xml configuration:
>>>  <?xml version="1.0" encoding="ISO-8859-1"?>
>>> <web-app 
>>> xmlns="http://java.sun.com/xml/ns/javaee";<http://java.sun.com/xml/ns/javaee>
>>>          
>>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";<http://www.w3.org/2001/XMLSchema-instance>
>>>          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
>>> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd";<http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
>>>          version="2.5">
>>>   <display-name>MyApp</display-name>
>>>
>>>      <listener>
>>>         
>>> <listener-class>eu.vranckaert.test.guice.GuiceContextListener</listener-class>
>>>     </listener>
>>>
>>>      <!-- GUICE -->
>>>     <filter>
>>>         <filter-name>guiceFilter</filter-name>
>>>         
>>> <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
>>>     </filter>
>>>
>>>      <filter-mapping>
>>>         <filter-name>guiceFilter</filter-name>
>>>         <url-pattern>/*</url-pattern>
>>>     </filter-mapping>
>>> </web-app>
>>>  
>>>  
>>>  When I do findById(..) I can also retrieve my record, so my DB 
>>> connection is working, I'm just not able to use the @Transactional 
>>> annotation... 
>>> So I found something about wrong scope of my EntityManager, but I have 
>>> no clue how I can change the entity manager's scope...
>>>
>>>  You guys have any idea what is wrong with my setup that the annotation 
>>> does not work?
>>>
>>>  Dirk
>>>  -- 
>>> 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 [email protected].
>>> To post to this group, send email to [email protected].
>>> 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 [email protected] <javascript:>.
> To post to this group, send email to [email protected]<javascript:>
> .
> 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to