【发布时间】:2020-11-07 01:56:27
【问题描述】:
我的测试应用程序的结构如下:
实体:
@Data
@Entity
@Table(name = "content")
public class Content {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String text;
}
存储库:
@Repository
public class ContentRepositoryCustomImpl implements ContentRepositoryCustom {
@PersistenceContext
private EntityManager entityManager;
@Override
public void updateLastChar(int batchSize, String lastChar) {
TypedQuery<Long> countQuery = entityManager.createQuery("Select count(c.id) from Content c", Long.class);
long count = countQuery.getSingleResult();
TypedQuery<Content> query = entityManager.createQuery("Select c From Content c", Content.class);
int pageNumber = 1;
while (pageNumber < count) {
query.setFirstResult(pageNumber - 1);
query.setMaxResults(batchSize);
List<Content> contents = query.getResultList();
for (Content content : contents) {
String temp = content.getText().substring(1);
content.setText(temp + lastChar);
}
pageNumber += batchSize;
}
}
}
测试:
@ExtendWith(SpringExtension.class)
@SpringBootTest
class ContentRepositoryTest {
@Autowired
ContentRepository contentRepository;
@Test
@Transactional
@Commit
public void testOOMError2() throws InterruptedException {
contentRepository.updateLastChar(30, "5");
}
}
application.yml:
spring:
datasource:
username: user
password: pass
url: jdbc:postgresql://localhost:5432/pg?currentSchema=myschema&characterEncoding=UTF-8&serverTimezone=UTC
driverClassName: org.postgresql.Driver
type: com.zaxxer.hikari.HikariDataSource
hikari:
maximumPoolSize: 20
jpa:
properties:
hibernate:
hbm2ddl:
auto: none
dialect: org.hibernate.dialect.PostgreSQL9Dialect
generate_statistics: true
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
数据库:
CREATE TABLE etl.content
(
id integer NOT NULL DEFAULT nextval('etl.content_seq'::regclass),
text character varying(1048512) COLLATE pg_catalog."default",
CONSTRAINT content_pkey PRIMARY KEY (id)
)
在数据库中,我有 100 条记录。每行的“文本”列已满(1048512 个字符)。
我在测试后得到的统计数据会不时变化。
当所有记录都更新时:
Session Metrics {
1365887 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
9047519 nanoseconds spent preparing 104 JDBC statements;
10297157763 nanoseconds spent executing 104 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
875610249 nanoseconds spent executing 1 flushes (flushing a total of 100 entities and 0 collections);
8352008089 nanoseconds spent executing 5 partial-flushes (flushing a total of 180 entities and 180 collections)
当一些记录被更新时(在下面的例子中是 79 个):
Session Metrics {
1566421 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
8680213 nanoseconds spent preparing 104 JDBC statements;
10392348834 nanoseconds spent executing 104 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
735904121 nanoseconds spent executing 1 flushes (flushing a total of 79 entities and 0 collections);
8773113348 nanoseconds spent executing 5 partial-flushes (flushing a total of 158 entities and 158 collections)
我试图弄清楚为什么会这样。 当我将刷新模式更改为 COMMIT 时,一切正常:
spring.jpa.properties.org.hibernate.flushMode=ALWAYS
但是,最好了解问题的原因。
更新 1:
我删除了
entityManager.persist(content);
正如 Simon Martinelli 建议的那样,但它仍然不起作用(更新了 69 个实体而不是 100 个):
Session Metrics {
11661208 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
18982347 nanoseconds spent preparing 105 JDBC statements;
8964494681 nanoseconds spent executing 105 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
774310803 nanoseconds spent executing 1 flushes (flushing a total of 69 entities and 0 collections);
7321581493 nanoseconds spent executing 5 partial-flushes (flushing a total of 138 entities and 138 collections)
更新 2:
我添加了 entityManager.flush():
public void updateLastChar(int batchSize, String lastChar) {
TypedQuery<Long> countQuery = entityManager.createQuery("Select count(c.id) from Content c", Long.class);
long count = countQuery.getSingleResult();
TypedQuery<Content> query = entityManager.createQuery("Select c From Content c", Content.class);
int pageNumber = 1;
while (pageNumber < count) {
query.setFirstResult(pageNumber - 1);
query.setMaxResults(batchSize);
List<Content> contents = query.getResultList();
for (Content content : contents) {
content.setText(lastChar);
}
entityManager.flush();
pageNumber += batchSize;
}
}
但问题仍然存在。此外,我的语句少于(65)条,少于 105 条(这种行为也时有发生):
Session Metrics {
8190152 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
5825900 nanoseconds spent preparing 65 JDBC statements;
39315983 nanoseconds spent executing 65 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
57656774 nanoseconds spent executing 5 flushes (flushing a total of 249 entities and 0 collections);
3436917 nanoseconds spent executing 5 partial-flushes (flushing a total of 129 entities and 129 collections)
【问题讨论】:
-
1) 为什么叫persist?必须在新实体上调用 Persist。 2) 您的测试是@Transactional,这会导致回滚,因此不会执行任何 SQL 语句。如果要执行语句,请使用 EntityManager.flush()
-
嗨@SimonMartinelli,感谢您的回复。 1)我已经删除了persist,但它并没有改变这种情况(见我的更新); 2)我也有 Commit 注释,所以它不应该回滚我的更改。我还在代码中添加了flush,不幸的是,它也没有帮助。
标签: java spring-boot hibernate jpa entitymanager