Stapler23
Stapler23

Reputation: 39

Oracle insertion with Java spring and hibernate takes too much time

I have a microservice that gets an array of objects from an API and then connects with a remote oracle BBDD and inserts the information. In a call, I'd get like 7k-10k registers and it's taking on average to save them 1 hour which is crazy. Why could it be? Is it a problem from the server or I'm doing something wrong?

application.properties file

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

The model of the table

@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 LOGIC

The logic is very simple

 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.");
        }

Repository

import org.springframework.data.jpa.repository.JpaRepository;

public interface callRepository extends JpaRepository<Call,Long> {
}

Upvotes: 2

Views: 834

Answers (1)

Sebastian C.
Sebastian C.

Reputation: 131

Even if you configured spring.jpa.properties.hibernate.jdbc.batch_size it won't help you because you are saving individually each entry of your list. use saveAll() method from JpaRepository and save in batches of 100 at least.

Upvotes: 1

Related Questions