簡體   English   中英

春天@transactional不起作用

[英]Spring @transactional doesn't work

我使用的是Java Configuration,除了@transactional之外,其他所有功能都正常運行,我花了很多時間試圖弄清楚,但不知道為什么它不起作用,任何幫助將不勝感激。

在UserController.java中,我嘗試調用userService.testTransactional(user,request);。

testTransaction()是UserServiceImpl中的測試方法,“ Long.valueOf(“ Throw RuntimeException”);“ 是將在此方法中引發異常的行,但是無論如何都會添加用戶,應該回滾,但用戶記錄仍然存在

UserController.java

@Controller
public class UserController {

    @Resource(name="userService")
    protected UserService userService;

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String processRegister(Model model, @ModelAttribute RegisterForm registerForm, HttpServletRequest request) throws Exception {

        userService.testTransactional(user, request);

        return "account/register_success";
    }
}

UserServiceImpl.java

@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    @Transactional
    public void testTransaction(User user, HttpServletRequest request) throws Exception {
        long userId = userDao.add(user);
        Long.valueOf("Throw RuntimeException");
    }
}

以下是我的配置

web.xml中

<!-- Java-based annotation-driven Spring container definition -->
<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>

<!-- Location of Java @Configuration classes that configure the components that makeup this application -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.myapp.config</param-value>
</context-param>


<!-- Secures the application -->
<filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher> 
</filter-mapping>

<!-- Handles requests into the application -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <!-- No explicit configuration file reference here: everything is configured in the root container for simplicity -->       
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

AppConfig.java

@Configuration  // Specifies the class as configuration
@EnableWebMvc //Enables to use Spring's annotations in the code
@ComponentScan(basePackages = {"com.myapp.account"}) // Specifies which package to scan
public class AppConfig {
    ...

DataConfig.java

@Configuration
@EnableTransactionManagement
public class DataConfig {

    @Bean(destroyMethod="close")
    public ComboPooledDataSource dataSource() throws PropertyVetoException {
        ...
        return cpds;
    }

    @Bean
    public NamedParameterJdbcTemplate jdbcTemplate() throws PropertyVetoException {
        return new NamedParameterJdbcTemplate(dataSource());
    }

    @Bean
    public DataSourceTransactionManager transactionManager() throws PropertyVetoException {
        DataSourceTransactionManager dstm = new DataSourceTransactionManager(dataSource());
        return dstm;
    }


    @Bean
    public UserDao userDao() throws PropertyVetoException {
        return new UserDao(jdbcTemplate());
    }

問題是您沒有在@Configuration類上指定@EnableTransactionManagement ,該類是組件掃描和創建UserServiceImpl bean的組件。 假設該類位於com.myapp.account ,則組件對其進行掃描的配置類應具有@EnableTransactionManagement 適當更改您的DataConfigAppConfig

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM