Spring @Transactional无法正常工作

使用自动有线Bean和较少的XML配置来重建项目,所以我正在重新研究此问题。我遵循了我以前的项目实现此方法的方式,但是没有用。有人可以为我提供帮助,为什么我应该做出什么改变?

我故意在插入用户详细信息方法中使用不存在的表名来故意引发异常。但是,不会回滚插入用户和插入用户角色的语句。请帮忙。

我当前的注册设计是这样的。

Part of servlet.xml:

<context:component-scan base-package="com.doyleisgod.golfer.controllers"/>

<context:component-scan base-package="com.doyleisgod.golfer.dao"/>

<context:component-scan base-package="com.doyleisgod.golfer.services"/>

<context:component-scan base-package="com.doyleisgod.golfer.validators"/>

Part of application context:

<context:annotation-config />

<tx:annotation-driven />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName" value="${jdbc.driverClassName}"/>

<property name="url" value="${jdbc.url}"/>

<property name="username" value="${jdbc.username}"/>

<property name="password" value="${jdbc.password}"/>

</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"/>

</bean>

Registration controller:

package com.doyleisgod.golfer.controllers;

import javax.validation.Valid;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.validation.BindingResult;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import com.doyleisgod.golfer.formdata.RegistrationForm;

import com.doyleisgod.golfer.services.IRegistrationService;

import com.doyleisgod.golfer.validators.RegistrationFormValidator;

/**

* Description: Registration controller provides and processes the registration form.

* @author Chris Doyle

*/

@Controller

@RequestMapping("/registration.htm")

public class RegistrationController {

protected final Log logger = LogFactory.getLog(getClass());

@Autowired private IRegistrationService iRegistrationService;

@Autowired private RegistrationFormValidator registrationFormValidator;

// sets a customer validator for the registration form

@InitBinder

protected void initBinder(WebDataBinder binder) {

binder.setValidator(registrationFormValidator);

}

// Description: Method called by a get request to the registration controller. Returns the

@RequestMapping(method=RequestMethod.GET)

public String registration (Model model){

model.addAttribute(new RegistrationForm());

return "registration";

}

// Description: Method called by a post request to the registration controller. Method calls validation on the registration form using custom validator and returning

// any errors back to the user.

@RequestMapping(method=RequestMethod.POST)

public String processRegistration (@Valid RegistrationForm registrationForm, BindingResult bindingResult, Model model){

logger.info("Received the following registration form details");

logger.info(registrationForm.toString());

if (bindingResult.hasErrors()) {

logger.warn("Registration Validation Failed");

model.addAttribute("validationError", "Please correct the fields marked with errors");

return "registration";

}

try {

iRegistrationService.registerUser(registrationForm);

} catch (Exception e) {

logger.error("An Exception has occured processing the registration form");

model.addAttribute("exceptionError", "An exception has occured, please try again.");

e.printStackTrace();

return "registration";

}

return "redirect:login.htm?registration=sucessful";

}

}

Registration service:

package com.doyleisgod.golfer.services;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.authentication.encoding.ShaPasswordEncoder;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import org.springframework.transaction.support.TransactionSynchronizationManager;

import com.doyleisgod.golfer.dao.IRegistrationDAO;

import com.doyleisgod.golfer.formdata.RegistrationForm;

@Service("IRegistrationService")

public class RegistrationService implements IRegistrationService {

@Autowired private IRegistrationDAO iRegistrationDAO;

private final boolean enabled = true;

private final String roles = "ROLE_USER";

@Override

@Transactional (rollbackFor = Exception.class)

public void registerUser(RegistrationForm registrationForm) throws Exception {

System.out.println("inside the registerUser method. is wrapped in transaction: "+TransactionSynchronizationManager.isActualTransactionActive());

String username = registrationForm.getUsername();

String password = registrationForm.getPassword();

String firstname = registrationForm.getFirstname();

String lastname = registrationForm.getLastname();

String email = registrationForm.getEmail();

int handicap = Integer.parseInt(registrationForm.getHandicap());

String encryptedPassword = ((new ShaPasswordEncoder()).encodePassword(password, username));

iRegistrationDAO.insertUser(username, encryptedPassword, enabled);

iRegistrationDAO.insertRoles(username, roles);

iRegistrationDAO.insertUserDetails(username, firstname, lastname, email, handicap);

}

@Override

public boolean checkUser(String username) {

return iRegistrationDAO.checkUserName(username);

}

}

Registration DAO:

package com.doyleisgod.golfer.dao;

import javax.annotation.Resource;

import org.apache.commons.dbcp.BasicDataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.support.TransactionSynchronizationManager;

@Repository("iRegistrationDAO")

public class RegistrationDAO extends JdbcTemplate implements IRegistrationDAO {

@Resource private BasicDataSource dataSource;

@Override

public boolean checkUserName(String username) {

int db_user = queryForInt("select count(username) from users where username = ?", username);

if (db_user == 1 ){

return true;

}

return false;

}

@Override

public void insertUser(String username, String password, boolean enabled) throws Exception {

System.out.println("inside the insertuser method. is wrapped in transaction: "+TransactionSynchronizationManager.isActualTransactionActive());

update("insert into users (username, password, enabled) VALUES (?,?,?)", username, password, enabled);

}

@Override

public void insertRoles(String username, String roles) throws Exception {

update("insert into user_roles (username, authority) VALUES (?,?)", username, roles);

}

回答:

context:component-scan标记移至应用程序上下文xml可以解决事务行为的原因是: <tx:annotation-driven />是一种后处理器,该处理器@Transactional使用处理事务行为的AOP方法拦截器包装带注释的Bean方法。Spring后处理器仅在定义它们的特定应用程序上下文上运行。

在你的情况下,你已<tx:annotation-driven />在应用程序上下文中定义了后处理器,而带有注释的Bean @Transactional在servlet应用程序上下文中。因此,<tx:annotation-driven />后处理器仅在应用程序上下文Bean上运行,而不在Servlet上下文Bean上运行。当context:component-scan标签被移到应用程序上下文中时,<tx:annotation-driven />后处理器会适当地包装其事务方法。

以上是 Spring @Transactional无法正常工作 的全部内容, 来源链接: utcz.com/qa/397904.html

回到顶部