【发布时间】:2017-08-11 13:13:18
【问题描述】:
这篇文章中的所有代码都可以在这里找到: https://github.com/cuipengfei/gs-accessing-data-jpa/tree/master/complete
您可以运行此测试来重现问题: https://github.com/cuipengfei/gs-accessing-data-jpa/blob/master/complete/src/test/java/hello/T1ServiceTest.java
我有一个领域模型:
@Entity
@Table(name = "t1", schema = "test")
public class T1 extends BaseEntity {
@Column(nullable = false)
private UUID someField;
@Column()
private ZonedDateTime date;
public T1(UUID someField, ZonedDateTime date) {
this.someField = someField;
this.date = date;
}
}
它有一个类型为 ZonedDateTime 的字段,所以我有一个转换器将其转换为 sql 时间戳:
@Converter(autoApply = true)
public class ZonedDateTimeAttributeConverter
implements AttributeConverter<ZonedDateTime, Timestamp> {
@Override
public Timestamp convertToDatabaseColumn(ZonedDateTime entityValue) {
return (entityValue == null) ? null :
valueOf(entityValue.withZoneSameInstant(of("UTC")).toLocalDateTime());
}
@Override
public ZonedDateTime convertToEntityAttribute(Timestamp databaseValue) {
return (databaseValue == null) ? null : databaseValue.toLocalDateTime().atZone(
of("UTC"));
}
}
当我尝试在这样的事务中创建大量 T1 时:
@Service
public class T1Service {
private static final Logger log = LoggerFactory.getLogger(T1Service.class);
@Autowired
T1Repository t1Repository;
@Transactional
public void insertMany() {
for (int i = 0; i < 1000; i++) {
log.info("!!! " + (i + 1) + "th item start");
UUID randomUUID = UUID.randomUUID();
T1 foundT1 = tryToFindExistingT1(randomUUID);//certainly won't find
if (foundT1 == null) {
log.info("t1 not found");
ZonedDateTime date = now();
//date = null;
//if you enable the line above, there won't be any update statements anymore
//and find will also become faster
T1 t1 = new T1(randomUUID, date);
saveT1(t1);
}
log.info("!!! " + (i + 1) + "th item finished");
log.info("====================================");
}
}
private T1 tryToFindExistingT1(UUID someField) {
long start = currentTimeMillis();
T1 t1Id = t1Repository.findBySomeField(someField);
//as nth item increases, the line above will become very very slow
//and also, there will be more and more update statements
//but if you set date of t1 to null, update statement will disappear and it'll not be slow
log.info("find took: " + (currentTimeMillis() - start) + " milliseconds");
return t1Id;
}
private T1 saveT1(T1 t1) {
long start = currentTimeMillis();
T1 savedT1 = t1Repository.save(t1);
log.info("save took: " + (currentTimeMillis() - start) + " milliseconds");
return savedT1;
}
}
hibernate会为整个t1表生成更新语句。
这是前几轮 for 循环的日志:
2017-03-20 17:51:54.039 INFO 74789 --- [ main] hello.T1Service : !!! 1th item start
2017-03-20 17:51:54.052 INFO 74789 --- [ main] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select t1x0_.id as id1_0_, t1x0_.date as date2_0_, t1x0_.some_field as some_fie3_0_ from test.t1 t1x0_ where t1x0_.some_field=?
2017-03-20 17:51:54.154 INFO 74789 --- [ main] hello.T1Service : find took: 114 milliseconds
2017-03-20 17:51:54.154 INFO 74789 --- [ main] hello.T1Service : t1 not found
2017-03-20 17:51:54.177 INFO 74789 --- [ main] hello.T1Service : save took: 15 milliseconds
2017-03-20 17:51:54.177 INFO 74789 --- [ main] hello.T1Service : !!! 1th item finished
2017-03-20 17:51:54.177 INFO 74789 --- [ main] hello.T1Service : ====================================
2017-03-20 17:51:54.177 INFO 74789 --- [ main] hello.T1Service : !!! 2th item start
Hibernate: insert into test.t1 (date, some_field, id) values (?, ?, ?)
Hibernate: update test.t1 set date=?, some_field=? where id=?
Hibernate: select t1x0_.id as id1_0_, t1x0_.date as date2_0_, t1x0_.some_field as some_fie3_0_ from test.t1 t1x0_ where t1x0_.some_field=?
2017-03-20 17:51:54.194 INFO 74789 --- [ main] hello.T1Service : find took: 17 milliseconds
2017-03-20 17:51:54.194 INFO 74789 --- [ main] hello.T1Service : t1 not found
2017-03-20 17:51:54.195 INFO 74789 --- [ main] hello.T1Service : save took: 1 milliseconds
2017-03-20 17:51:54.195 INFO 74789 --- [ main] hello.T1Service : !!! 2th item finished
2017-03-20 17:51:54.195 INFO 74789 --- [ main] hello.T1Service : ====================================
2017-03-20 17:51:54.195 INFO 74789 --- [ main] hello.T1Service : !!! 3th item start
Hibernate: insert into test.t1 (date, some_field, id) values (?, ?, ?)
Hibernate: update test.t1 set date=?, some_field=? where id=?
Hibernate: update test.t1 set date=?, some_field=? where id=?
Hibernate: select t1x0_.id as id1_0_, t1x0_.date as date2_0_, t1x0_.some_field as some_fie3_0_ from test.t1 t1x0_ where t1x0_.some_field=?
2017-03-20 17:51:54.200 INFO 74789 --- [ main] hello.T1Service : find took: 4 milliseconds
2017-03-20 17:51:54.200 INFO 74789 --- [ main] hello.T1Service : t1 not found
2017-03-20 17:51:54.200 INFO 74789 --- [ main] hello.T1Service : save took: 0 milliseconds
2017-03-20 17:51:54.200 INFO 74789 --- [ main] hello.T1Service : !!! 3th item finished
2017-03-20 17:51:54.200 INFO 74789 --- [ main] hello.T1Service : ====================================
2017-03-20 17:51:54.200 INFO 74789 --- [ main] hello.T1Service : !!! 4th item start
Hibernate: insert into test.t1 (date, some_field, id) values (?, ?, ?)
Hibernate: update test.t1 set date=?, some_field=? where id=?
Hibernate: update test.t1 set date=?, some_field=? where id=?
Hibernate: update test.t1 set date=?, some_field=? where id=?
Hibernate: select t1x0_.id as id1_0_, t1x0_.date as date2_0_, t1x0_.some_field as some_fie3_0_ from test.t1 t1x0_ where t1x0_.some_field=?
2017-03-20 17:51:54.209 INFO 74789 --- [ main] hello.T1Service : find took: 9 milliseconds
2017-03-20 17:51:54.209 INFO 74789 --- [ main] hello.T1Service : t1 not found
2017-03-20 17:51:54.210 INFO 74789 --- [ main] hello.T1Service : save took: 1 milliseconds
2017-03-20 17:51:54.210 INFO 74789 --- [ main] hello.T1Service : !!! 4th item finished
2017-03-20 17:51:54.210 INFO 74789 --- [ main] hello.T1Service : ====================================
从日志中可以看到,hibernate会生成越来越多的更新语句。
看起来更新语句的数量总是与 T1 等待提交的行数相同。
现在如果我移除转换器,这个问题就会消失。
我可以用hibernate-java8 lib代替这个转换器来达到同样的效果,但是为什么会这样呢?
为什么JPA AttributeConverter 让hibernate 在事务中对整个表生成更新语句?
【问题讨论】:
-
一切都在一个事务中,每个对象都被添加到一级缓存中。在执行查询之前,hibernate 会对一级缓存中的所有对象进行脏检查。由于属性转换器,它会检测到字段上的更改(出于某种原因),因此需要在执行查询之前对其进行更新。随着一级缓存中项目数量的增加,更新的数量也在增加。
-
@M.Deinum 说得通。但我仍然对为什么休眠会将数据识别为脏数据感到困惑。自创建以来,日期从未更改。
-
我怀疑由于转换器的性质,hibernate 提供了默认的转换器(5.2 默认支持此功能,无需创建转换器或使用 hibernate-java8 库)。特定于休眠的一个比您在此处显示的那个更详细。这也是为什么使用默认类型而不是转换器时错误消失的原因。
标签: java spring hibernate jpa spring-data-jpa