Spring事务没有提交

时间:2010-06-07 19:37:11

标签: java hibernate spring transactions

我正在努力让我的春季管理交易得到承诺,有人可以发现我做错了什么。我的所有表都是mysql InnonDB表。我的RemoteServiceServlet(GWT)如下:

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;
 }

DAO如下:

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);
}

我的applicationContext.xml如下:

<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"/>

如果我将sessionFactory属性更改为autoCommit = true,那么我的对象确实会被持久化。

<prop key="hibernate.connection.autocommit">true</prop>

3 个答案:

答案 0 :(得分:4)

当GWT调用createUser时,此调用不会通过Spring transactional proxy。为了使用@Transactional,您可以引入更多级别的间接:

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) { 
        ...
    }
}

编辑:您也可以使用spring4gwt代替它。

答案 1 :(得分:2)

我有一个类似的问题,事务没有提交,因此在行上创建锁,直到我发现我有另一个DAO(其中包含@transactional注释)没有参与事务,但仍然在事务dao之前被调用。  我清理了那个dao,它后来工作。

答案 2 :(得分:0)

使用PROPAGATION_NOT_SUPPORTED,如下所示:

public CustomerReportEntity publishReportInTransaction(final CustomerReportEntity report) {
        final TransactionTemplate transactionTemplate = new TransactionTemplate(this.transactionManagerWDB);
                transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
        return (CustomerReportEntity) transactionTemplate.execute(new TransactionCallback() {
            @Override
            public CustomerReportEntity doInTransaction(final TransactionStatus transactionStatus) {
                try {
                    return generateReportinTransaction(report);

                } catch (final Exception e) {
                    report.setReportStatus(ReportStatus.EXCEPTION.getStatus());
                    pendingProcessorDao.updateStatus(report, "Exception");
                    return null;
                }
            }
        });
    }