I do not have either of the above, although I do have <tx:annotation-driven/> in my spring xml file that I am refrencing in my JavaConfig file using @ImportResource.
But I need to have it otherwise Spring is not detecting the classes annotated with @TransactionAttribute.
This is what I have tried :
TRY 1:
I just define the following beans in my Config.java file :
Code:
private @Value("#{jdbcProperties['jdbc.driver']}") String jdbcDriver;
private @Value("#{jdbcProperties['jdbc.url']}") String url;
private @Value("#{jdbcProperties['jdbc.username']}") String username;
private @Value("#{jdbcProperties['jdbc.password']}") String password;
private @Value("#{jdbcProperties['jdbc.showSql']}") Boolean showSql;
public @Bean BasicDataSource dataSource() {
System.out.println("jdbcDriver is :" + jdbcDriver);
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(jdbcDriver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
public @Bean(name="entityManagerFactory") @DependsOn({"dataSource"})EntityManagerFactory entityManagerFactory(){
LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setPersistenceXmlLocation("classpath:META-INF/persistence.xml");
entityManagerFactory.setPersistenceUnitName("testTransaction");
entityManagerFactory.setDataSource(dataSource());
HibernateJpaVendorAdapter jpaAdaptor = new HibernateJpaVendorAdapter();
jpaAdaptor.setShowSql(showSql);
jpaAdaptor.setGenerateDdl(true);
jpaAdaptor.setDatabasePlatform("org.hibernate.dialect.HSQLDialect");
entityManagerFactory.setJpaVendorAdapter(jpaAdaptor);
//This is the most important line as now it will not be called by Spring and we have to call it ourself
entityManagerFactory.afterPropertiesSet();
return entityManagerFactory.getObject();
}
public @Bean(name="transactionManager") JpaTransactionManager transactionManager(){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory());
return transactionManager;
}
public @Bean PersistenceAnnotationBeanPostProcessor beanPostProcessor(){
PersistenceAnnotationBeanPostProcessor processor = new PersistenceAnnotationBeanPostProcessor();
return processor;
}
.......
and my test Service bean that has @TransactionAttribute present on it.
It fails first with the NullPointerException at the following line :
Code:
jpaAdaptor.setShowSql(showSql);
where showSql is being read from a properties file using @Value annotation.
TRY 2: I hard code all the values for JDBC access and try again.
My dataSource bean now looks like :
Code:
public @Bean BasicDataSource dataSource() {
System.out.println("jdbcDriver is :" + jdbcDriver);
BasicDataSource dataSource = new BasicDataSource();
// dataSource.setDriverClassName(jdbcDriver);
// dataSource.setUrl(url);
// dataSource.setUsername(username);
// dataSource.setPassword(password);
dataSource.setDriverClassName("org.hsqldb.jdbc.JDBCDriver");
dataSource.setUrl("jdbc:hsqldb:mem:.");
dataSource.setUsername("sa");
dataSource.setPassword("");
return dataSource;
}
Rest all remains same. It fails with error :
Code:
Exception in thread "main" javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:419)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
at $Proxy14.flush(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:224)
at $Proxy14.flush(Unknown Source)
at org.example.dao.model.TestDao.insert(TestDao.java:29)
at org.example.service.TestService.testMethod(TestService.java:32)
at org.example.transaction.config.LoadConfiguration.main(LoadConfiguration.java:17)
finally I introduce <tx:annotation-driven/> in my spring.xml file and and things start working again. Although @Value still doesnt work. I have to remove the PersistenceAnnotationBeanPostProcessor bean in order to have it working again.
Thanks,
Anuj