我在执行带有约束违例异常的批处理插入时遇到错误,对我而言这没有任何意义。
我从表中获取用户列表,然后在审核表中查找这些用户的条目。如果不存在,请插入它们。因为要处理大量用户,所以我对要插入的数据进行批处理,创建对象列表,然后使用 saveAll(list)进行批处理。
我正在使用Spring Boot 2.1.3和Hikari 3.3.1。获取插入内容的表的主键是数据库生成的ID字段。因此,除非可能是时间问题,否则尚不清楚如何获得重复的输入错误。
我一次批处理1000个对象/语句。倒转前至少工作1000次。我假设“托管刷新”错误是重复输入错误的副作用。
我的实体是:
@Data
@Entity
@Table(name = "event_state", schema = "subscription")
public class EventState {
@Id
@GeneratedValue(generator = "generator")
@GenericGenerator(name = "generator", strategy = "increment")
private Long id;
@Column(name = "customer_dimension_id")
private Long customerDimensionId;
@Column(name = "event_id")
private Long eventId;
@Column(name = "last_modified")
@Temporal(TemporalType.TIMESTAMP)
private Date lastModified;
}
这是 application.properties 文件(因为我已经掌握了,所以将一些驱动程序属性附加到url行):
spring.output.ansi.enabled=ALWAYS
# logging
logging.file=/tmp/activeusers.log
#HikariCP config
subscription.datasource.url=jdbc:mysql://mysqlhost:3306/subscription?rewriteBatchedStatements=true&cachePrepStmts=true&prepStmtCacheSize=250&prepStmtCacheSqlLimit=2048&useServerPrepStmts=true
subscription.datasource.username=USER
subscription.datasource.password=PASS
subscription.datasource.driverClassName=com.mysql.cj.jdbc.Driver
subscription.datasource.hikari.connection-timeout=60000
subscription.datasource.hikari.maximum-pool-size=6
subscription.datasource.hikari.idle-timeout=300000
subscription.datasource.hikari.max-lifetime=1800000
subscription.datasource.hikari.minimum-idle=6
subscription.datasource.hikari.auto-commit=true
subscription.datasource.hikari.validation-timeout=15000
subscription.datasource.cachePrepStmts=true
subscription.datasource.prepStmtCacheSize=250
subscription.datasource.prepStmtCacheSqlLimit=2048
subscription.datasource.useServerPrepStmts=true
subscription.datasource.useLocalSessionState=true
subscription.datasource.rewriteBatchedStatements=true
subscription.datasource.cacheResultSetMetadata=true
subscription.datasource.cacheServerConfiguration=true
subscription.datasource.elideSetAutoCommits=true
subscription.datasource.maintainTimeStats=false
subscription.datasource.hikari.pool-name=SubscriptionPool
#HikariCP config
activity.datasource.url=jdbc:mysql://mysqlhost:3306/activity
activity.datasource.username=USER
activity.datasource.password=PASS
activity.datasource.driverClassName=com.mysql.cj.jdbc.Driver
activity.datasource.hikari.connection-timeout=60000
activity.datasource.hikari.maximum-pool-size=6
activity.datasource.hikari.idle-timeout=300000
activity.datasource.hikari.max-lifetime=1800000
activity.datasource.hikari.minimum-idle=6
activity.datasource.hikari.auto-commit=true
activity.datasource.hikari.validation-timeout=15000
activity.datasource.cachePrepStmts=true
activity.datasource.prepStmtCacheSize=250
activity.datasource.prepStmtCacheSqlLimit=2048
activity.datasource.useServerPrepStmts=true
activity.datasource.useLocalSessionState=true
activity.datasource.rewriteBatchedStatements=true
activity.datasource.cacheResultSetMetadata=true
activity.datasource.cacheServerConfiguration=true
activity.datasource.elideSetAutoCommits=true
activity.datasource.maintainTimeStats=false
activity.datasource.hikari.pool-name=ActivityPool
spring.jpa.properties.hibernate.jdbc.batch_size=500
spring.jpa.properties.hibernate.order_inserts=true
我最初将 batch_size 设置为1000,然后将其更改为500。
UPD 1:尝试从循环使用 saveAll 更改为 save 。同样的问题。
欢迎并赞赏所有想法!