Save using Spring Data Jpa and find using JdbcTemplate within same transaction - spring-data-jpa

When i comment the line 276, The code in the line 277 is not working. Within the same transaction i am saving via data jpa and querying same table data via jdbctemplate. Please help me how to make it working line277 by commenting the line276.
saving via data jpa
.
querying via jdbctemplate

Related

Nested Query in spring batch processing

I want to create an ETL process using Spring Batch, the steps will be read from a DB(s) and insert in one DB so basically I'm collecting similar information from different DB and inserting them in one DB, I have a large complex query that I need to run on those DBs and the result will be inserted in the so called one DB for later processing, my main concert is that I want to reference this query in the JpaPagingItemReader for example, it there a way I can for example add this query in my project as .sql file and then reference it in the reader?
Or any other solution I can follow?
Thank you
it there a way I can for example add this query in my project as .sql file and then reference it in the reader? Or any other solution I can follow?
You can put your query in a properties file and inject in your reader, something like:
#Configuration
#EnableBatchProcessing
#PropertySource("classpath:application.properties")
public class MyJob {
#Bean
public JpaPagingItemReader itemReader(#Value("${query}") String query) {
return new JpaPagingItemReaderBuilder<>()
.queryString(query)
// set other reader properties
.build();
}
// ...
}
In this example, you should have a property query=your sql query in application.properties. This is actually the regular Spring property injection mechanism, nothing Spring Batch specific here.

How to implement batch insert using spring-data-jdbc

is it possible to implement batch insert using spring-data-jdbc somehow? Or can i get access to JDBCTemplate using this spring-data realization?
There is currently no support for batch operations.
There are two issues requesting that one might want to follow if one is interested in that feature: https://jira.spring.io/browse/DATAJDBC-328 and https://jira.spring.io/browse/DATAJDBC-314
If one is working with Spring Data JDBC there will always be a NamedParameterJdbcTemplate in the application context so one can get that injected in order to perform batch operations without any additional configuration.

No Exception when Spring batch tables are not created

If the BATCH_JOB_EXECUTION_CONTEXT was not created all I get is :
Caused by: org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [INSERT INTO BATCH_JOB_EXECUTION_CONTEXT (SHORT_CONTEXT, SERIALIZED_CONTEXT, JOB_EXECUTION_ID) VALUES(?, ?, ?)]; nested exception is java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist
the error above is displayed while inserting into the table, but at the moment of the creation of the table (done automatically by spring batch )no error was displayed.
I would like to know the reason way the table was not created so I could know what is the issue (database permissions, problems of tablespace...) is it possible to log it in traces?
for information I'm using oracle database 12C and ojdbc8 version 12.2.0.1
Spring Batch does not automatically create meta-data tables in your datasource. You need to run the tables creation script against your database manually.
However, if you use Spring Boot, those tables can be created automatically using the spring.batch.initialize-schema property. More details here: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-initialize-a-spring-batch-database
There are similar/related questions to this one, I'm adding them for reference here:
Spring Batch Framework - Auto create Batch Table
Spring Batch doesn't use custom datasource to create tables
Check your Spring Boot starter class or configuration class and make sure you are not excluding this auto configuration:
(exclude={BatchAutoConfiguration.class})
Also make sure this property is not set to never:
spring.batch.initialize-schema

spring batch transaction manager clarification

we have two diff datasource (one for spring batch and one for biz domain). When we configure spring batch job, where does transaction manager supposed to reference. Is it datasource pointing to spring batch schema or biz schema?
I would say you should use same transaction manager for your data and meta-data, so that they are consistent.
Standard SB transaction manager is the one pointed by bean named transactionManager.
If you have more than one transaction manager in your spring config, you'll need to specify the name of the bean you want to use.
If you have more than one datasource, one for SB metadata tables and one for processed data, you need distribuited transaction.

spring-data-mongodb. How can i dynamically create a database in mongo using spring-data-mongodb library?

spring-data-mongodb. How can i dynamically create a database in mongo using spring-data-mongodb library?
I am trying to use Spring-Mongodb-Data module for CRUD operations against Mongo database and going through examples and articles my assumption is that databasename should be pre-defined in spring context xml when defining MongoTemplate bean.
In my case I have an multi-tenant application that will accept requests over http and my application should create the mongodatabase on-the-fly and use the name provided in the input http request to create the database and then load the data into collection in the newly created database.
I am trying to figure out if there is a way to dynamically populate the databasename in MongoTemplate or MongoRepository without having to provide it in spring context.xml?
Please help me.
Thanks
-RK
Have you tried the following instead of going through the pre-defined spring context configuration.
MongoTemplate getMongoTemplate(Mongo mongo, String database) {
return new MongoTemplate(mongo, database);
}