我正在努力让我的spring管理的事务提交,有人能帮忙看看我做错了什么吗?我的所有表都是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>