All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mageddo.tobby.producer.ProducerJdbc Maven / Gradle / Ivy

There is a newer version: 2.1.6-alpha
Show newest version
package com.mageddo.tobby.producer;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mageddo.tobby.ProducedRecord;
import com.mageddo.tobby.ProducerRecord;
import com.mageddo.tobby.RecordDAO;
import com.mageddo.tobby.UncheckedSQLException;
import com.mageddo.tobby.internal.utils.StopWatch;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ProducerJdbc implements Producer {

  private final RecordDAO recordDAO;
  private final DataSource dataSource;

  public ProducerJdbc(RecordDAO recordDAO, DataSource dataSource) {
    this.recordDAO = recordDAO;
    this.dataSource = dataSource;
  }

  @Override
  public ProducedRecord send(ProducerRecord record) {
    final StopWatch totalStopWatch = StopWatch.createStarted();
    final StopWatch commitStopWatch = new StopWatch();
    try (Connection connection = this.dataSource.getConnection();) {
      final boolean autoCommit = connection.getAutoCommit();
      final ProducedRecord r = this.recordDAO.save(connection, record);
      if (!autoCommit) {
        commitStopWatch.start();
        connection.commit();
        commitStopWatch.stop();
      }
      return r;
    } catch (SQLException e) {
      throw new UncheckedSQLException(e);
    } finally {
      if (log.isTraceEnabled()) {
        log.trace("status=committed, commit={}, total={}", commitStopWatch.getTime(), totalStopWatch.getTime());
      }
    }
  }

  @Override
  public ProducedRecord send(Connection connection, ProducerRecord record) {
    return this.recordDAO.save(connection, record);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy