批量插入大量数据
-
问题
最近使用Spring-Boot遇到一个问题,当向数据库批量插入数据时,如果数据量过大会报错
MutationState size is bigger than maximum allowed number of bytes
-
解决
@PersistenceContext
private EntityManager em;
@Override
@Transactional
public <T> void saveList(List<T> list) throws SQLException {
if (list == null || list.isEmpty()) {
return ;
}
int size = list.size();
Connection connection = em.unwrap(SessionImpl.class).connection();
for (int i = 0; i < list.size(); ++i) {
System.out.println("正在保存第:" + i + "条数据*************************************");
em.persist(list.get(i));
if (i % 10000 == 0 || i == list.size()) {
em.flush();
em.clear();
connection.commit();
}
}
}
网友评论