Spring并不认识@Transactional注释

时间:2017-05-17 11:52:40

标签: java spring spring-mvc spring-transactions transactional

我需要一些帮助。 Spring似乎没有认识到我用@Transactional注释的方法。我看了很多解决方案的来源但却找不到解决方案。 我需要注释dao方法,而不是服务(我知道这是最佳实践)。 附:抱歉我的英语不好。

我的appInitializer:

package com.dreamteam.datavisualizator.common.configurations;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{ServletContext.class};
    }

    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{ApplicationContext.class};
    }

    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

我的servletContext:

package com.dreamteam.datavisualizator.common.configurations;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@ComponentScan(basePackages = "com.dreamteam.datavisualizator")
@EnableWebMvc
@EnableTransactionManagement
public class ServletContext extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setViewClass(JstlView.class);
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

我的applicationContext:

package com.dreamteam.datavisualizator.common.configurations;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
public class ApplicationContext {

    @Bean(name = "dataSource")
    public DataSource getDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        dataSource.setUrl(System.getenv("SQL_JDBC_URL"));
        dataSource.setUsername(System.getenv("SQL_LOGIN"));
        dataSource.setPassword(System.getenv("SQL_PASSWORD"));
        return dataSource;
    }

    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(getDataSource());
    }

    @Bean(name="generalTemplate")
    public JdbcTemplate getJdbcTemplate(){
        return new JdbcTemplate(getDataSource());
    }

    @Bean(name="simpleCallTemplate")
    public SimpleJdbcCall getSimpleJdbcCall(){
        return new SimpleJdbcCall(getDataSource());
    }
}

我的Dao类(在这个方法中可以更多调用SimpleJdbcCall /但是例如):

@Repository("userDaoImpl")
public class UserDAOImpl implements UserDAO {
    private enum UserColumnName {ID, FIRST_NAME, LAST_NAME, EMAIL}

    @Autowired
    private JdbcTemplate generalTemplate;

    @Autowired
    private SimpleJdbcCall simpleCallTemplate;

    //...

    @Transactional
    public BigInteger createObject(BigInteger object_id, String name) {
        simpleCallTemplate.withFunctionName(INSERT_OBJECT);
        SqlParameterSource in = new MapSqlParameterSource()
                .addValue("obj_type_id", object_id)
                .addValue("obj_name", name);
        return simpleCallTemplate.executeFunction(BigDecimal.class, in).toBigInteger();
    }

    //...

    private String INSERT_OBJECT = "insert_object";
}

2 个答案:

答案 0 :(得分:0)

也许是因为Open Session? https://vladmihalcea.com/the-open-session-in-view-anti-pattern/

在属性文件中尝试: spring.jpa.open式视=假

答案 1 :(得分:0)

您的配置似乎是正确的。我相信它没有工作,因为你没有指定rollbackFor

@Transactional(value = "transactionManager", rollbackFor = java.lang.Exception.class)
public BigInteger createObject(BigInteger object_id, String name) {
    simpleCallTemplate.withFunctionName(INSERT_OBJECT);
    SqlParameterSource in = new MapSqlParameterSource()
            .addValue("obj_type_id", object_id)
            .addValue("obj_name", name);
    return simpleCallTemplate.executeFunction(BigDecimal.class, in).toBigInteger();
}

现在,如果您的方法中出现java.lang.Exception类型的异常,它将回滚所有更改