views:

153

answers:

2

I am struggling to get my spring managed transactions to commit, could someone please spot what I have done wrong. All my tables are mysql InnonDB tables. My RemoteServiceServlet (GWT) is as follows:

public class TrainTrackServiceImpl extends RemoteServiceServlet implements TrainTrackService {

 @Autowired
 private DAO dao;

 @Override
 public void init(ServletConfig config) throws ServletException {
  super.init(config);
  WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext());
  AutowireCapableBeanFactory beanFactory = ctx.getAutowireCapableBeanFactory();
  beanFactory.autowireBean(this);
 }

    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
 public UserDTO createUser(String firstName, String lastName,
   String idNumber, String cellPhone, String email, int merchantId) {
  User user = new User();

  user.setFirstName(firstName);
  user.setLastName(lastName);
  user.setIdNumber(idNumber);
  user.setCellphone(cellPhone);
  user.setEmail(email);
        user.setDateCreated(new Date());

  Merchant merchant = (Merchant) dao.find(Merchant.class, merchantId);
  if (merchant != null) {
   user.setMerchant(merchant);
  }

  // Save the user.
  dao.saveOrUpdate(user);
  UserDTO dto = new UserDTO();
  dto.id = user.getId();
  dto.firstName = user.getFirstName();
  dto.lastName = user.getLastName();
  return dto;
 }

The DAO is as follows:

public class DAO extends HibernateDaoSupport {

private String adminUsername;
private String adminPassword;
private String godUsername;
private String godPassword;

public String getAdminUsername() {
    return adminUsername;
}

public void setAdminUsername(String adminUsername) {
    this.adminUsername = adminUsername;
}

public String getAdminPassword() {
    return adminPassword;
}

public void setAdminPassword(String adminPassword) {
    this.adminPassword = adminPassword;
}

public String getGodUsername() {
    return godUsername;
}

public void setGodUsername(String godUsername) {
    this.godUsername = godUsername;
}

public String getGodPassword() {
    return godPassword;
}

public void setGodPassword(String godPassword) {
    this.godPassword = godPassword;
}

public void saveOrUpdate(ModelObject obj) {
    getHibernateTemplate().saveOrUpdate(obj);
}

And my applicationContext.xml is as follows:

<context:annotation-config/>

<context:component-scan base-package="za.co.xxx.traintrack.server"/>

<!-- Application properties -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>file:${user.dir}/@propertiesFile@</value>
        </list>
    </property>
</bean>

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${connection.dialect}</prop>
            <prop key="hibernate.connection.username">${connection.username}</prop>
            <prop key="hibernate.connection.password">${connection.password}</prop>
            <prop key="hibernate.connection.url">${connection.url}</prop>
            <prop key="hibernate.connection.driver_class">${connection.driver.class}</prop>
            <prop key="hibernate.show_sql">${show.sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.c3p0.min_size">5</prop>
            <prop key="hibernate.c3p0.max_size">20</prop>
            <prop key="hibernate.c3p0.timeout">300</prop>
            <prop key="hibernate.c3p0.max_statements">50</prop>
            <prop key="hibernate.c3p0.idle_test_period">60</prop>
        </props>
    </property>
    <property name="annotatedClasses">
        <list>
            <value>za.co.xxx.traintrack.server.model.Answer</value>
            <value>za.co.xxx.traintrack.server.model.Company</value>
            <value>za.co.xxx.traintrack.server.model.CompanyRegion</value>
            <value>za.co.xxx.traintrack.server.model.Merchant</value>
            <value>za.co.xxx.traintrack.server.model.Module</value>
            <value>za.co.xxx.traintrack.server.model.Question</value>
            <value>za.co.xxx.traintrack.server.model.User</value>
            <value>za.co.xxx.traintrack.server.model.CompletedModule</value>
        </list>
    </property>
</bean>

<bean id="dao" class="za.co.xxx.traintrack.server.DAO">
    <property name="sessionFactory" ref="sessionFactory"/>
    <property name="adminUsername" value="${admin.user.name}"/>
    <property name="adminPassword" value="${admin.user.password}"/>
    <property name="godUsername" value="${god.user.name}"/>
    <property name="godPassword" value="${god.user.password}"/>
</bean>

<bean id="transactionManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref local="sessionFactory"/>
    </property>
</bean>

<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="transactionManager"/>

If I change the sessionFactory property to be autoCommit=true then my object does get persisited.

<prop key="hibernate.connection.autocommit">true</prop>
+2  A: 

When GWT calls createUser, this call doesn't go through a Spring's transactional proxy. In order to use @Transactional you can introduce additional level of indirection:

public class TrainTrackServiceWrapper extends RemoteServiceServlet implements TrainTrackService { 

    @Autowired 
    private TrainTrackServiceImpl impl; 

    ...

    public UserDTO createUser(String firstName, String lastName, 
        String idNumber, String cellPhone, String email, int merchantId) { 

        return impl.createUser(firstName, lastName, idNumber, cellPhone, email, merchantId);
    }

}

public class TrainTrackServiceImpl implements TrainTrackService { 

    @Autowired 
    private DAO dao; 

    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class) 
    public UserDTO createUser(String firstName, String lastName, 
       String idNumber, String cellPhone, String email, int merchantId) { 
        ...
    }
}

EDIT: You may also use spring4gwt instead of it.

axtavt
Thanks, you are a legend
Clinton Bosch
+1  A: 

I had a similar problem with transaction not committing therefore creating locks on rows, until I found that I had another DAO (with @transactional annotations in it) that was not participating in the transaction but nevertheless was called before transactional dao. I cleaned up that dao and it worked afterwords.

Alexander