使用 Java spring 和休眠的 Oracle 插入需要太多时间
Posted
技术标签:
【中文标题】使用 Java spring 和休眠的 Oracle 插入需要太多时间【英文标题】:Oracle insertion with Java spring and hibernate takes too much time 【发布时间】:2020-10-02 00:35:13 【问题描述】:我有一个微服务,它从 API 获取一组对象,然后连接到远程 oracle BBDD 并插入信息。在一个电话中,我会得到大约 7k-10k 个寄存器,平均要花 1 个小时来节省它们,这太疯狂了。为什么会这样?是服务器的问题还是我做错了什么?
application.properties 文件
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.datasource.url= jdbc:oracle:thin:url
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.jpa.hibernate.ddl-auto=none
hibernate.jdbc.batch_size=50
spring.jpa.properties.hibernate.default_schema=schema
spring.jpa.show-sql=true
spring.data.rest.basePath=/api
spring.jpa.hibernate.connection.provider_class=org.hibernate.hikaricp.internal.HikariCPConnectionProvider
spring.datasource.hikari.minimumIdle=5
spring.datasource.hikari.maximumPoolSize=20
spring.datasource.hikari.idleTimeout=600000
spring.datasource.hikari.poolName=springBootJPAHikariCP
spring.datasource.hikari.maxLifetime=2000000
spring.datasource.hikari.connectionTimeout=10000
桌子的型号
@Entity
@Table(name = "CALL")
public class Call
@Column(name = "ID")
@Id
private String id;
@Column(name = "QUEUEID")
private String queueId;
@Column(name = "QUEUETYPE")
private String queueType;
@Column(name = "AGENTID")
private String agentId;
@Column(name = "DATASET")
private String dataSet;
@Column(name = "CALLDATE")
private Timestamp callDate;
@Column(name = "CALLDURATION")
private String callDuration;
@Column(name = "RINGTIME")
private String ringTime;
@Column(name = "RESULT")
private String result;
@Column(name = "DATE_LOAD")
private Date fecCarga = new Date();
//GETTERS AND SETTERS
Java 逻辑
逻辑很简单
ObjectMapper objectMapper = new ObjectMapper();
String response = reportingServices.getToken();
Token token = objectMapper.readValue(response, Token.class);
String tokenString = token.setToken(token.getToken());
String startDate = req.getStartDate();
String endDate = req.getEndDate() ;
final String uri = API_URL
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(uri, String.class);
JSONObject json = new JSONObject(result);
JSONArray jsonArray = json.getJSONArray("list");
try
for (int i = 0, size = jsonArray.length(); i < size; i++)
JSONObject objectInArray = jsonArray.getJSONObject(i);
Call call = new Call();
String callid = (String) objectInArray.get("callid");
String qid = (String) objectInArray.get("qid");
String type = (String) objectInArray.get("type");
String agent = (String) objectInArray.get("agent");
String dataset = (String) objectInArray.get("dataset");
String datetime = (String) objectInArray.get("datetime");
String duration = (String) objectInArray.get("duration");
String ringtime = (String) objectInArray.get("ringtime");
String resultTable = (String) objectInArray.get("result");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date parsedDate = dateFormat.parse(datetime);
Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
call.setId(callid);
call.setQueueId(qid);
call.setQueueType(type);
call.setAgentId(agent);
call.setDataSet(dataset);
call.setCallDate(timestamp);
call.setCallDuration(duration);
call.setRingTime(ringtime);
call.setResult(resultTable);
call.setFecCarga(call.getFecCarga());
callRepository.save(call);
return ResponseHandler.generateResponse(HttpStatus.CREATED, "Info created successfully");
catch (Exception e)
return ResponseHandler.generateResponse(HttpStatus.INTERNAL_SERVER_ERROR, "Something went wrong.");
存储库
import org.springframework.data.jpa.repository.JpaRepository;
public interface callRepository extends JpaRepository<Call,Long>
【问题讨论】:
您介意分享callRepository
吗?这是哪个版本的 Spring Boot?
我刚刚用callRepository
更新了帖子。 2.2.6版
【参考方案1】:
即使您配置了spring.jpa.properties.hibernate.jdbc.batch_size
,它也无济于事,因为您要单独保存列表中的每个条目。使用 JpaRepository 中的saveAll()
方法,至少批量保存 100 个。
【讨论】:
但是如果您阅读代码,在每次迭代中都会保存一个寄存器,因为我必须遍历一组对象。是否可以将saveAll()
与我实现的逻辑一起使用?
是的。在 for 循环之前创建一个 ListsaveAll
方法在哪里呢?
if 语句的内部 for 循环,该语句检查您已迭代的元素数量。另一种方法是同时执行它们,甚至在那里也使用批量插入。也许这可以帮助你:persistencelayer.wixsite.com/springboot-hibernate/post/…以上是关于使用 Java spring 和休眠的 Oracle 插入需要太多时间的主要内容,如果未能解决你的问题,请参考以下文章
Spring数据休眠upsert事务错误@TransactionalEventListener和@Transactinal