I need some help. Spring doesn't seem to recognize my methods which was annotated with @Transactional. I watched a lot of sources for solution but couldn't find one. And I need to annotate exactly dao methods, not services(I know that this is best practice). P.S. Sorry for my bad english.
My 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[]{"/"};
}
}
My 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();
}
}
My 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());
}
}
My Dao class (in this method can be more calls of SimpleJdbcCall/ but that for example):
@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";
}
Your configuration seems to be correct. I believe it's not working because you did not specify 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();
}
Now if an exception of type java.lang.Exception occurs in your method, it'll rollback all changes