问题描述
目前我正在使用 JpaItemWriter 编写对象列表,如下所示,工作正常.现在由于性能问题,我想将 JpaItemWriter 更改为 JdbcBatchItemWriter.
Currently i am using JpaItemWriter to write the list of objects as below which is working fine.Now i want to change the JpaItemWriter to JdbcBatchItemWriter due to performance issue.
public class MyItemWriter implements ItemWriter<List<MyDomainObject>> {
@Override
public void write(List<? extends Lists<MyDomainObject>> items) {
JpaItemWriter<MyDomainObject> writer = new JpaItemWriter<>();
for(List<MyDomainObject> o : items)
{
writer.write(o);
}
}
}
建议使用 JdbcBatchItemWriter 编写对象列表的示例片段将有所帮助.尝试使用 ItemSqlParameterSourceProvider 它并没有帮助结束 org.springframework.dao.InvalidDataAccessApiUsageException: No value provided for the SQL parameter
异常
Suggest a sample snippets which uses the JdbcBatchItemWriter to write the List of objects will helps. Tried using the ItemSqlParameterSourceProvider it did't help ending up in org.springframework.dao.InvalidDataAccessApiUsageException: No value supplied for the SQL parameter
exception
推荐答案
你的例子不正确.您正在 write
方法中创建一个 JpaItemWriter
,因此每次调用 write
时都会创建一个新实例.这可能是导致您出现性能问题的原因.
You example is not correct. You are creating a JpaItemWriter
in the write
method, so a new instance is created on each call to write
. This is probably the cause of your performance issue.
更重要的是,委托编写者的生命周期方法(打开/更新/关闭)将不会被尊重(对于没有实现 ItemStream
的 JpaItemWriter
则不是这种情况但如果委托是项目流,这将是一个问题).你的 MyItemWriter
实现应该是这样的:
More importantly, lifecycle methods of the delegate writer (open/update/close) will not be honored (it is not the case for JpaItemWriter
which does not implement ItemStream
but this would be a problem if the delegate is an item stream). Your MyItemWriter
implementation should be something like:
public class MyItemWriter implements ItemWriter<List<MyDomainObject>> {
private JpaItemWriter jpaItemWriter;
public MyItemWriter(JpaItemWriter jpaItemWriter) {
this. jpaItemWriter = jpaItemWriter;
}
@Override
public void write(List<? extends Lists<MyDomainObject>> items) {
for(List<MyDomainObject> o : items) {
this. jpaItemWriter.write(o);
}
}
}
现在,如果您想使用 JdbcBatchItemWriter
编写列表列表,请参阅 Spring Batch - 使用带有列表列表的 ItemWriter.
Now if you want to use the JdbcBatchItemWriter
to write a list of lists, see Spring Batch - Using an ItemWriter with List of Lists.
添加了如何根据评论中的要求设置委托的示例代码:
Added a sample code of how to set the delegate as requested in comments:
@Bean
public ListUnpackingItemWriter<T> itemWriter() {
JdbcBatchItemWriter<T> jdbcBatchItemWriter = null; // configure your jdbcBatchItemWriter
ListUnpackingItemWriter<T> listUnpackingItemWriter = new ListUnpackingItemWriter<>();
listUnpackingItemWriter.setDelegate(jdbcBatchItemWriter);
return listUnpackingItemWriter;
}
这篇关于使用 JdbcBatchItemWriter 编写项目列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!