lundi 30 mars 2015

Spring @Transactional not working In standalone application

I have a standalone application in which I am trying to populate a database. My application config looks like:



@Configuration
@EnableTransactionManagement
@PropertySource(value = { "classpath:classpath property file" })
@ComponentScan(basePackages = { my packages to scan })
public class PersistenceJPAConfig {

@Bean
public EntityManagerFactory entityManagerFactoryBean() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { packages to scan});

JpaVendorAdapter vendorAdapter = jpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
em.afterPropertiesSet();

return em.getObject();
}

@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setDatabase(Database.ORACLE);
return jpaVendorAdapter;
}

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUrl(url);
dataSource.setUsername(dbUser);
dataSource.setPassword(dbPassword);
return dataSource;
}

@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactoryBean());

return transactionManager;
}

@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}

Properties additionalProperties() {
return new Properties() {
{ // Hibernate Specific:
setProperty("hibernate.hbm2ddl.auto", "validate");
setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
}
};
}
}


However when I am trying to persist something in database, the annotation is failing with error: Exception in thread "main" javax.persistence.TransactionRequiredException: No transactional EntityManager available


In the resulting error stack, I don't see the transactional advice being applied.


The file that contains transactional annotation is something like this:



import org.springframework.transaction.annotation.Transactional;

@Service
public class MyService {

@Autowired
private MyDao myDao;

@Transactional
public void save(MyEntity e) {
myDao.save(e);
}
}


The MyDao class is something like this:



@Repository
public class MyDao {
@PersistenceContext
private EntityManager entityManager;

public void save(MyEntity e) {
entityManager.persist(e);
}

}


I am calling the function from my Main method as follows:



public static void main(String[] args) {

try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext()) {
ctx.scan(CONFIG_PACKAGE);
ctx.refresh();
myService = ctx.getBean(MyService.class);
Thread.sleep(10 * 1000);
myService.save(entity);
...
}


Can anyone please help me with this, i.e. what I might be doing wrong? Thanks for your help


Aucun commentaire:

Enregistrer un commentaire