Trying to use Spring with JPA and JTA with only Spring Annotation - spring-data-jpa

I am setting up an application that accept a REST call and then does some DB work. I am trying to use Spring to do everything via annotations, except I would like to keep the persistence.xml file. However, I am being thrown an error that there is not a Bean called the unitName I have added to the #PersistenceContext.
#Repository
#Transactional
public class UserController implements Serializable {
private static final long serialVersionUID = 4625223295588959802L;
#PersistenceContext(unitName="PerfJPASpring")
private EntityManager em;
#Transactional
public void addUser(User user) {
em.persist(user);
}
#Transactional
public void deleteAll() {
em.createNamedQuery("User.deleteAll").executeUpdate();
}
#Transactional
public List<User> selectAllRecords() {
return em.createNamedQuery("User.findAll").getResultList();
}
}
The other issue I have is how do I get Spring to use the persistence.xml and not define the DataSource in the LocalContainerEntityManagerFactoryBean. I have added the Datasource to the App Server.
#Configuration
#EnableTransactionManagement
#EnableJpaRepositories(basePackageClasses=UserController.class)
public class DatabaseConfig {
#Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
EclipseLinkJpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
vendorAdapter.setShowSql(false);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.example.jpa.model");
factory.setDataSource(dataSource());
Properties jpaProps = new Properties();
jpaProps.put("eclipselink.weaving", "false");
factory.setJpaProperties(jpaProps);
return factory;
}
#Bean
public JpaTransactionManager transactionManager() throws SQLException {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory().getObject());
return txManager;
}
public DataSource dataSource() {
try {
Context initCtx = new InitialContext();
return (DataSource) initCtx.lookup("jdbc/userdb");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Unable to lookup datasource", e);
}
}
}
The unitName does match that in the persistence.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="PerfJPASpring" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/userdb</jta-data-source>
<class>com.example.jpa.model.User</class>
</persistence-unit>
</persistence>
And this is the error I am receiving.
Uncaught init() exception created by servlet [DispatcherServlet] in application [PerfJPASpring]: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'PerfJPASpring' is defined
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:357)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:540)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:244)
at [internal classes]
Could point me in the right direction for Spring to use the unitName and how I can avoid specifying the datasource in the Db Config?
Thanks.

I found the issue. First, since I am using JTA, then I can remove #Transactional and remove the Datasource and replace it with the setPersistenceUnitName. Now it uses the persistence.xml file and JTA.
The Service:
#Repository
public class UserController implements Serializable {
private static final long serialVersionUID = 4625223295588959802L;
#PersistenceContext(unitName="PerfJPASpring")
private EntityManager em;
public void addUser(User user) {
em.persist(user);
}
public void deleteAll() {
em.createNamedQuery("User.deleteAll").executeUpdate();
}
public List<User> selectAllRecords() {
return em.createNamedQuery("User.findAll").getResultList();
}
}
Database Config:
#Configuration
#EnableJpaRepositories(basePackageClasses=UserController.class)
public class DatabaseConfig {
#Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
EclipseLinkJpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
vendorAdapter.setShowSql(false);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.example.jpa.model");
factory.setPersistenceUnitName("PerfJPASpring");
Properties jpaProps = new Properties();
jpaProps.put("eclipselink.weaving", "false");
factory.setJpaProperties(jpaProps);
return factory;
}
#Bean
public JpaTransactionManager transactionManager() throws SQLException {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory().getObject());
return txManager;
}
}

Related

Java SpringBoot defining multiple datasources error

I am using springBoot, and am following this tutorial while trying to set up two datasources (pims & powwow).
application.properties
# pims datasource
spring.datasource1.jdbc-url=jdbc:postgresql://localhost:5432/pims
spring.datasource1.username=postgres
spring.datasource1.password=postgres
# powwow datasource
spring.datasource2.jdbc-url=jdbc:postgresql://localhost:5432/powwow
spring.datasource2.username=postgres
spring.datasource2.password=postgres
PersistencePimsAutoConfiguration.java
#Configuration
#PropertySource({"classpath:application.properties"})
#EnableJpaRepositories(
basePackages = {"com.clubtravel.powwow.dao.pims", "com.clubtravel.powwow.repositories.pims"},
entityManagerFactoryRef = "pimsEntityManager",
transactionManagerRef = "pimsTransactionManager")
public class PersistencePimsAutoConfiguration {
#Autowired
private Environment env;
#Primary
#Bean
#ConfigurationProperties(prefix="spring.datasource1")
public DataSource pimsDataSource() {
return DataSourceBuilder.create().build();
}
#Primary
public LocalContainerEntityManagerFactoryBean pimsEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(pimsDataSource());
em.setPackagesToScan(new String[] { "com.clubtravel.powwow.entities.pims" });
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto",env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect",env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
#Bean
public PlatformTransactionManager pimsTransactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(pimsEntityManager().getObject());
return transactionManager;
}
}
PersistencePowwowAutoConfiguration.java
#Configuration
#PropertySource({"classpath:application.properties"})
#EnableJpaRepositories(
basePackages = {"com.clubtravel.powwow.dao.powwow", "com.clubtravel.powwow.repositories.powwow"},
entityManagerFactoryRef = "powwowEntityManager",
transactionManagerRef = "powwowTransactionManager")
public class PersistencePowwowAutoConfiguration {
#Autowired
private Environment env;
#Primary
#Bean
#ConfigurationProperties(prefix="spring.datasource2")
public DataSource powwowDataSource() {
return DataSourceBuilder.create().build();
}
#Bean
#Primary
public LocalContainerEntityManagerFactoryBean powwowEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(powwowDataSource());
em.setPackagesToScan(new String[] { "com.clubtravel.powwow.entities.powwow" });
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto",env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect",env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
#Bean
public PlatformTransactionManager powwowTransactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(powwowEntityManager().getObject());
return transactionManager;
}
}
LobDao.java
#Component
public class LobDao {
#Autowired
LobRepository lobRepository;
#PersistenceContext
EntityManager entityManager;
public List<LobEntity> findAll() {
return lobRepository.findAll();
}
LobRepository.java
#Repository
public interface LobRepository extends JpaRepository<LobEntity,Integer> {
}
When i start the server, I get the folowing:
Error
***************************
APPLICATION FAILED TO START
***************************
Description:
Field lobRepository in com.clubtravel.powwow.dao.pims.LobDao required a bean of type 'com.clubtravel.powwow.repositories.pims.LobRepository' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.clubtravel.powwow.repositories.pims.LobRepository' in your configuration.
I have partitioned the entities, daos and repositories for each separate database.
More info:
If I add the repository package:
basePackages = {"com.clubtravel.powwow.dao.pims", "com.clubtravel.powwow.repositories.pims"},
The error becomes:
Description:
Field lobRepository in com.clubtravel.powwow.dao.pims.LobDao required a bean named 'pimsEntityManager' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean named 'pimsEntityManager' in your configuration.
More info:
I have also tried adding '#Transactional' to the DAO, but it makes no difference:
#Transactional("pimsTransactionManager")
public List<LobEntity> findAll() {
return lobRepository.findAll();
}
I had such problem, and it resolved by just adding these annotationsannotations in the main springBoot application class:
#SpringBootApplication(scanBasePackages = "com.clubtravel.powwow")
#EntityScan("com.clubtravel.powwow")
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}

Multithreading with StoredProcedureItemReader

Is it possible to have multiple threads with StoredProcedureItemReader? I have done multithreading with PageReader but not sure if it will work with StoredProcedureItemReader.
Below is the job configuration for using a StoredProcedureReader. I have wrapped the reader in Thread safe reader. I want to use ThreadPoolTaskExecutor but not able to figure out how I can do partition for each thread with the Stored procedure.
***#Configuration
public class SpPocJobConfigurationMT {
private DataSource dataSource;
/**
* The Job builder factory.
*/
private JobBuilderFactory jobBuilderFactory;
/**
* The Jdbc template.
*/
#Autowired
JdbcTemplate jdbcTemplate;
/**
* The Step builder factory.
*/
private StepBuilderFactory stepBuilderFactory;
#Autowired
private BillingRecordAuditRepository billingRecordAuditRepository;
#Autowired
private StagingMortgageDataTxnRepository stagingMortgageDataTxnRepository;
private SystemRepository systemRepository;
#Autowired
public SpPocJobConfigurationMT(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory, SystemRepository systemRepository, DataSource dataSource) {
Assert.notNull(systemRepository, "SystemRepository cannot be null");
Assert.notNull(jobBuilderFactory, "JobBuilderFactory cannot be null");
Assert.notNull(stepBuilderFactory, "StepBuilderFactory cannot be null");
Assert.notNull(dataSource, "DataSource cannot be null");
this.jobBuilderFactory = jobBuilderFactory;
this.stepBuilderFactory = stepBuilderFactory;
this.systemRepository = systemRepository;
this.dataSource = dataSource;
}
#Bean
#Transactional
#Description(value = "")
public Job SpPocJobMT() throws Exception {
return jobBuilderFactory.get("spPocJobMT")
.start(spPocStepMT())
.build();
}
#Bean
public Step spPocStepMT() throws Exception {
return stepBuilderFactory.get("spPocStepMT")
.allowStartIfComplete(false)
.<StagingDataDto,StagingDataDto> chunk(20)
.reader(sybcSpReaderMT())
.processor(spPocProcessorMT())
.writer(spPocWriterMT())
// .taskExecutor(new ThreadPoolTaskExecutor ())
// .taskExecutor(new SimpleAsyncTaskExecutor())
.build();
}
#Bean
public SpPocWriter spPocWriterMT() {
return new SpPocWriter(this.billingRepository, this.stagingTxnRepository);
}
#Bean
public SpPocProcessor spPocProcessorMT() {
return new SpPocProcessor();
}
#Bean
#StepScope
public SynchronizedItemStreamReader sybcSpReaderMT() {
StoredProcedureItemReader reader = new StoredProcedureItemReader();
SqlParameter[] parameters = {new SqlParameter("#p_id", OracleTypes.NUMBER)
, new SqlOutParameter("#p_out_c1", OracleTypes.CURSOR)
, new SqlOutParameter("#p_out_c2", OracleTypes.CURSOR)
};
reader.setDataSource(dataSource);
reader.setProcedureName("SP_POC_FINAL");
reader.setRowMapper(new SPRowMapper());
reader.setRefCursorPosition(3);
reader.setPreparedStatementSetter(new MyItemPreparedStatementSetter());
reader.setParameters(parameters);
reader.setSaveState(false);
reader.setVerifyCursorPosition(false);
SynchronizedItemStreamReader synchronizedItemStreamReader = new SynchronizedItemStreamReader();
synchronizedItemStreamReader.setDelegate(reader);
return synchronizedItemStreamReader;
}
public class MyItemPreparedStatementSetter implements PreparedStatementSetter {
#Override
public void setValues(PreparedStatement ps) throws SQLException {
ps.setInt(1, 1);
((CallableStatement) ps).registerOutParameter(2, OracleTypes.CURSOR);
((CallableStatement) ps).registerOutParameter(3, OracleTypes.CURSOR);
}
}
}***
The StoredProcedureItemReader extends AbstractItemCountingItemStreamItemReader which is not thread-safe, please check its javadoc.
If you want to use the StoredProcedureItemReader in a multi-threaded step, you need to wrap it in a SynchronizedItemStreamReader or make it step-scoped.

How to run update query in Spring JPA for quartz job

I have a quartz job in spring 4 and I am using JPA hibernate to update database value through quartz job but I am getting javax.persistence.TransactionRequiredException: Executing an update/delete query
I don't understand what kind of configuration is missing in quartz job. I referred to SpringBeanAutowiringSupport example still update is failing but select is working fine.
Below is my code
#Configuration
#ComponentScan("com.stock")
public class QuartzConfiguration {
#Autowired
private ApplicationContext applicationContext;
#Bean
public JobDetailFactoryBean jobDetailBalanceCarryForward(){
JobDetailFactoryBean factory = new JobDetailFactoryBean();
factory.setJobClass(BillingCroneSvcImpl.class);
Map<String,Object> map = new HashMap<String,Object>();
map.put("task", "balanceCarryForward");
factory.setJobDataAsMap(map);
factory.setGroup("BalanceCarryForwardJob");
factory.setName("balance carry forward");
return factory;
}
#Bean
public CronTriggerFactoryBean cronTriggerBalanceCarryForward(){
CronTriggerFactoryBean stFactory = new CronTriggerFactoryBean();
stFactory.setJobDetail(jobDetailBalanceCarryForward().getObject());
stFactory.setStartDelay(3000);
stFactory.setName("balancCarryForwardTrigger");
stFactory.setGroup("balanceCarryForwardgroup");
stFactory.setCronExpression("0 0/1 * 1/1 * ? *");
return stFactory;
}
#Bean
public SpringBeanJobFactory springBeanJobFactory() {
AutoWiringSpringBeanJobFactory jobFactory = new AutoWiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
return jobFactory;
}
#Bean
public SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
schedulerFactory.setJobFactory(springBeanJobFactory());
schedulerFactory.setTriggers(cronTriggerBalanceCarryForward().getObject());
return schedulerFactory;
}
}
Below class where quartz executeInternal method is written
#Service
#PersistJobDataAfterExecution
#DisallowConcurrentExecution
#Autowired
private BillingCroneRepo billingCroneRepo;
public class BillingCroneSvcImpl extends QuartzJobBean implements BillingCroneSvc {
#Override
#Transactional
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(context);
billingCroneRepo.updateBalance();
// this method throws exception javax.persistence.TransactionRequiredException: Executing an update/delete query
}
}
App config class
#EnableWebMvc
#EnableTransactionManagement
#Configuration
#ComponentScan({ "com.stock.*" })
#Import({ SecurityConfig.class })
#PropertySource("classpath:jdbc.properties")
public class AppConfig extends WebMvcConfigurerAdapter {
private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";
private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";
#Resource
private Environment env;
#Bean(name = "dataSource")
public DriverManagerDataSource dataSource() {
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
driverManagerDataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
driverManagerDataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
driverManagerDataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));
return driverManagerDataSource;
}
#Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistence.class);
entityManagerFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
entityManagerFactoryBean.setJpaProperties(hibProperties());
return entityManagerFactoryBean;
}
private Properties hibProperties() {
Properties properties = new Properties();
properties.put(PROPERTY_NAME_HIBERNATE_DIALECT,env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL,env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
return properties;
}
#Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
#Bean
public ReloadableResourceBundleMessageSource messageSource(){
ReloadableResourceBundleMessageSource messageSource=new ReloadableResourceBundleMessageSource();
String[] resources= {"classpath:messages"};
messageSource.setBasenames(resources);
return messageSource;
}
#Bean
public LocaleResolver localeResolver() {
final CookieLocaleResolver ret = new CookieLocaleResolver();
ret.setDefaultLocale(new Locale("en_IN"));
return ret;
}
#Bean
public LocaleChangeInterceptor localeChangeInterceptor(){
LocaleChangeInterceptor localeChangeInterceptor=new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
return localeChangeInterceptor;
}
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/Angular/**").addResourceLocations("/Angular/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/email_templates/**").addResourceLocations("/email_templates/");
registry.addResourceHandler("/fonts/**").addResourceLocations("/fonts/");
registry.addResourceHandler("/img/**").addResourceLocations("/img/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/Landing_page/**").addResourceLocations("/Landing_page/");
}
#Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
org.springframework.core.io.Resource[] resources = new ClassPathResource[] { new ClassPathResource("application.properties") };
pspc.setLocations(resources);
pspc.setIgnoreUnresolvablePlaceholders(true);
return pspc;
}
#Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/pages/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
// through below code we directly read properties file in jsp file
#Bean(name = "propertyConfigurer")
public PropertiesFactoryBean mapper() {
PropertiesFactoryBean bean = new PropertiesFactoryBean();
bean.setLocation(new ClassPathResource("application.properties"));
return bean;
}
}
Can anybody please assist me how to resolve transational issue in spring JPA with quartz
Thanks you all for your help. Finally I autowired EntityManagerFactory instead of persitance EntityManager and it is working fine. I tried all scenario but nothing worked to inject spring transactional in quartz so finally autoriwed entitymanagerfactory
Below is my repo class code.
#Repository
public class BillingCroneRepoImpl implements BillingCroneRepo {
/*#PersistenceContext
private EntityManager entityManager;*/
#Autowired
EntityManagerFactory entityManagerFactory;
public boolean updateTable(){
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();
entityTransaction.begin(); // this will go in try catch
Query query = entityManager.createQuery(updateSql);
// update table code goes here
entityTransaction.commit(); // this will go in try catch
}
}
I'm not the Spring specialist, but I think new ... doesn't work with #Transactional
#Service
#PersistJobDataAfterExecution
#DisallowConcurrentExecution
public class BillingCroneSvcImpl extends QuartzJobBean implements BillingCroneSvc {
#Autowired
BillingCroneRepo billingCroneRepo;
#Override
#Transactional
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(context);
billingCroneRepo.updateBalance();
}
}
It's because quartz is using the bean instead of the proxy generated for #Transactional.
Use either MethodInvokingJobDetailFactoryBean (instead of inheriting QuartzJob) or use a dedicated wrapper quarz bean (inheriting from QuartzJob) that call the spring bean (not inheriting from QuartzJob) having the #Transactionnal annotation.
EDIT : this is in fact not the problem
The problem is here :
JobDetailFactoryBean factory = new JobDetailFactoryBean();
factory.setJobClass(BillingCroneSvcImpl.class);
By passing the class, I presume that Quartz will instantiate it itself, so Spring won't create it and won't wrap the bean in a Proxy that handle the #Transactionnal behaviour.
Instead you must use something along the line :
#Bean(name = "billingCroneSvc")
public BillingCroneSvc getSvc(){
return new BillingCroneSvcImpl();
}
#Bean
public JobDetailFactoryBean jobDetailBalanceCarryForward(){
JobDetailFactoryBean factory = new JobDetailFactoryBean();
getSvc();// just make sure the bean is instantiated
factory.setBeanName("billingCroneSvc");
...
}

Spring-boot jpa only can access primary from muliple data sources

Trying to use 2 data sources as below inside spring-boot:
DB configs:
//no primary
#Configuration
#EnableTransactionManagement
#EnableJpaRepositories(
entityManagerFactoryRef = "rwEntityManagerFactory",
transactionManagerRef = "rwTransactionManager",
basePackages = {"com.cvs.dvp.repository.rw"})
#PropertySource("classpath:application.properties")
public class RwDbConfiguration {
#Bean(name="rwDataSource")
//#Primary
#ConfigurationProperties(prefix="datasource.rw")
public DataSource rwDataSource() {
return DataSourceBuilder.create().build();
}
#Bean(name="rwEntityManagerFactory")
#Qualifier("rwEntityManagerFactory")
public EntityManagerFactory rwEntityManagerFactory() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setShowSql(true);
jpaVendorAdapter.setGenerateDdl(false);
jpaVendorAdapter.setDatabase(Database.ORACLE);
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
lef.setDataSource(rwDataSource());
lef.setJpaVendorAdapter(jpaVendorAdapter);
lef.setPackagesToScan("com.cvs.dvp.domain.rw");
lef.setPersistenceUnitName("rw");
lef.afterPropertiesSet();
return lef.getObject();
}
#Bean(name="rwTransactionManager")
#Qualifier("rwTransactionManager")
public JpaTransactionManager transactionManager(#Qualifier("rwEntityManagerFactory")EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
}
And //primary
#Configuration
#EnableTransactionManagement
#EnableJpaRepositories(
entityManagerFactoryRef = "dsEntityManagerFactory",
transactionManagerRef = "dsTransactionManager",
basePackages = {"com.cvs.dvp.repository.dsd"})
#PropertySource("classpath:application.properties")
public class DsdDbConfiguration {
#Bean(name="dsDataSource")
#Primary
#ConfigurationProperties(prefix="datasource.ds")
public DataSource dsDataSource() {
return DataSourceBuilder.create().build();
}
#Bean(name="dsEntityManagerFactory")
#Qualifier("dsEntityManagerFactory")
public EntityManagerFactory dsEntityManagerFactory( ) {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setShowSql(true);
jpaVendorAdapter.setGenerateDdl(true);
jpaVendorAdapter.setDatabase(Database.ORACLE);
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
lef.setDataSource(dsDataSource());
lef.setJpaVendorAdapter(jpaVendorAdapter);
lef.setPackagesToScan("com.cvs.dvp.domain.dsd");
lef.setPersistenceUnitName("dsd");
lef.afterPropertiesSet();
return lef.getObject();
}
#Bean(name="dsTransactionManager")
#Qualifier("dsTransactionManager")
#Primary
public JpaTransactionManager transactionManager(#Qualifier("dsEntityManagerFactory")EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
}
The services are:
#Service
public class ReceiptServiceImpl implements ReceiptService {
private static final Logger LOGGER = LoggerFactory.getLogger(ReceiptServiceImpl.class);
private final ReceiptRepository repository;
#Autowired
#PersistenceContext(unitName = "rw")
#Qualifier("rwTransactionManager")
private JpaTransactionManager jpaTransactionManager;
#Inject
public ReceiptServiceImpl(final ReceiptRepository repository) {
this.repository = repository;
}
#Override
#Transactional("rwTransactionManager")
public Receipt save(#NotNull #Valid final Receipt receipt) {
EntityManagerFactory emf = jpaTransactionManager.getEntityManagerFactory();
EntityManager entityManager = emf.createEntityManager();
LOGGER.debug("Creating {}", receipt.getId());
Receipt existing = repository.findOne(receipt.getStoreNumber());
if (existing != null) {
throw new UserAlreadyExistsException(
String.format("There already exists a receipt with id=%s", receipt.getId()));
}
return repository.save(existing);
}
#Override
#Transactional("rwTransactionManager")
public List<Receipt> getList() {
LOGGER.debug("Retrieving the list of all users");
return repository.findAll();
}
#Override
#Transactional("rwTransactionManager")
public Receipt getOne(String storeNumber) {
return repository.findOne(storeNumber);
}
}
And
#Service
#Validated
public class StatusServiceImpl implements StatusService {
private static final Logger LOGGER = LoggerFactory.getLogger(StatusServiceImpl.class);
private final StatusRepository repository;
#Autowired
#Qualifier("dsTransactionManager")
private JpaTransactionManager jpaTransactionManager;
#Inject
public StatusServiceImpl(final StatusRepository repository) {
this.repository = repository;
}
#Override
#Transactional
public Status save(#NotNull #Valid final Status status) {
EntityManagerFactory emf = jpaTransactionManager.getEntityManagerFactory();
EntityManager entityManager = emf.createEntityManager();
LOGGER.debug("Creating {}", status);
Status existing = repository.findOne(status.getStatusCd());
if (existing != null) {
throw new UserAlreadyExistsException(
String.format("There already exists a user with id=%s", status.getStatusCd()));
}
return repository.save(status);
}
#Override
#Transactional
public List<Status> getList() {
LOGGER.debug("Retrieving the list of all users");
return repository.findAll();
}
}
The issue: bean wiring is ok, but only primary datasource can be accessed, another datasource got "table or view does not existed" error, but if it been configed as primary everything works fine and previous primary datasource will not be accessable.
Tried to make none of them primary as post Multiple jpa:repositories in xml config, how to configure with #EnableJPARepositories using Spring java config?, but would not pass spring-boot compiler.
Spring CLI version is 1.2.1.
Appreciate your help.

Spring batch use JpaPagingItemReader JpaItemWriter no transaction is in progress

My project uses java config, the DataSource entityManagerFactory PlatformTransactionManager created by other java config file.
When ItemReader read data, ItemProcessor data transformed ItemWriter has some error
How can I solve this problem?
Error message
2015-01-17 16:26:30.390 ERROR AbstractStep:225 - Encountered an error executing step testStep in job validJob
javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.checkTransactionNeeded(AbstractEntityManagerImpl.java:1171)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1332)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
How can I enable transactions on the following Code:
#Configuration
#EnableBatchProcessing
public class JobConfiguration {
#Inject
private PlatformTransactionManager transactionManager;
#Inject
private EntityManagerFactory emf;
#Bean
public Job validJob(JobBuilderFactory jobBuilderFactory,Step step) {
return jobBuilderFactory.get("validJob").flow(step).end().build();
}
#Bean
public Step testStep(StepBuilderFactory stepBuilderFactory, ItemReader<PurchaseOrder> reader,
ItemWriter<PurchaseOrder> writer, ItemProcessor<PurchaseOrder, PurchaseOrder> processor) {
return stepBuilderFactory.get("testStep")//
.transactionManager(transactionManager)//
.<PurchaseOrder, PurchaseOrder>chunk(100)//
.reader(reader)//
.processor(processor)//
.writer(writer)//
.build();
}
#Bean
public ItemReader<PurchaseOrder> reader() {
JpaPagingItemReader<PurchaseOrder> reader = new JpaPagingItemReader<PurchaseOrder>();
reader.setEntityManagerFactory(emf);
reader.setQueryString(" select p from PurchaseOrder p ");
reader.setTransacted(false);
return reader;
}
#Bean
public ItemProcessor<PurchaseOrder, PurchaseOrder> processor() {
return new PurchaseOrderItemProcessor();
}
#Bean
public ItemWriter<PurchaseOrder> writer() {
JpaItemWriter<PurchaseOrder> writer = new JpaItemWriter<PurchaseOrder>();
writer.setEntityManagerFactory(emf);
return writer;
}