package io.trygvis.queue;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class SqlEffectExecutor {
private final DataSource dataSource;
public SqlEffectExecutor(DataSource dataSource) {
this.dataSource = dataSource;
}
public A transaction(SqlEffect effect) throws SQLException {
// int pid;
try (Connection c = dataSource.getConnection()) {
// pid = getPid(c);
// System.out.println("pid = " + pid);
boolean ok = false;
try {
A a = effect.doInConnection(c);
c.commit();
ok = true;
return a;
} finally {
// System.out.println("Closing, pid = " + pid);
if (!ok) {
try {
c.rollback();
} catch (SQLException e) {
// ignore
}
}
}
}
}
public void transaction(final SqlEffect.Void effect) throws SQLException {
transaction(new SqlEffect