I have a spring-batch-boot application geared toward an SqlServer DB.
The spring-boot part consists of repositories (for instance:
@Repository
public interface **Repository extends CrudRepository<***Entity, Long> {
}
)
and corresponding entities (for instance:
@Data
@Entity
@Generated
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Accessors(chain = true)
@Table(name = "DYN_***")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class ***Entity extends FileStructure{
@Id
@GeneratedValue
private Long id;
@NonNull
@Column(name = "***", length = 2, nullable = false)
private String recordId;
...
)
If I run the app, the correct tables are created. But if I call save, only this is printed:
Hibernate: select next_val as id_val from ***.dbo.hibernate_sequence with (updlock, rowlock)
Hibernate: update ***.dbo.hibernate_sequence set next_val= ? where next_val=?
meaning the entity is not saved!
This happens if my Repository is a CrudRepository
.
If it is a JpaRepository
and I call saveAndFlush
, I get the exception: Caused by: javax.persistence.TransactionRequiredException: no transaction is in progress
, even if the method/class/both are annotated by @Transactional
The entire config is:
@Slf4j
@Configuration
@EnableBatchProcessing
public class BatchConfig extends DefaultBatchConfigurer {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Resource
***Repository ***Repository;
@Autowired
private Writer itemWriter;
@Bean
public Job processJob() {
return jobBuilderFactory.get("processJob")
.incrementer(new RunIdIncrementer())
.flow(orderStep1()).end().build();
}
@Bean
public Step orderStep1() {
return stepBuilderFactory.get("orderStep1").<String, String>chunk(1)
.reader(new Reader()).processor(new Processor())
.writer(itemWriter).build();
}
}
With the yml:
spring:
batch:
auto-commit: true
initialize-schema: always
job:
enabled: true
main:
allow-bean-definition-overriding: true
devtools:
add-properties: false
profiles: dev
datasource:
url: jdbc:sqlserver://*****:**;DatabaseName=****;integratedSecurity=true
#driver_class_name: com.microsoft.sqlserver.jdbc.SQLServerDriver
driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
hikari:
maximum-pool-size: 50
connection-test-query: SELECT 1
validation-timeout: 30000
minimum-idle: 5
max-lifetime: 300
jpa:
database: SQL_SERVER
database-platform: org.hibernate.dialect.SQLServerDialect
show-sql: true
hibernate:
ddl-auto: update
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
properties:
hibernate:
default_schema: dbo
default_catalog: ***
#generate_statistics: true
order_inserts: true
jdbc:
batch_size=4:
batch:
initialize-schema: always
server:
servlet:
context-path: *****
port: 12345
max-http-header-size: 50000
My writer class where the save
is called:
@Slf4j
@Service
@Component
@Configuration
@Transactional
public class Writer implements ItemWriter<String> {
...
And the startup class is simply:
@SpringBootApplication
@EnableBatchProcessing
public class SpringBatchApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBatchApplication.class, args);
}
}
What can be the problem?
- I have to say that this log is written at the start of the application:
No database type set, using meta data indicating: SQLSERVER [traceId=/spanId=]
EDIT: It seems there are a few other answers, but I haven't found one answer that suits me, with good JPA and yml support.