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

io.github.afezeria.freedao.classic.runtime.context.TransactionContext Maven / Gradle / Ivy

package io.github.afezeria.freedao.classic.runtime.context;

import io.github.afezeria.freedao.classic.runtime.ResultHandler;
import io.github.afezeria.freedao.classic.runtime.SqlExecutor;
import io.github.afezeria.freedao.classic.runtime.SqlSignature;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;

/**
 *
 */
public class TransactionContext extends DaoContext {

    DataSource dataSource;

    public TransactionContext(DataSource dataSource) {
        Objects.requireNonNull(dataSource);
        this.dataSource = dataSource;
    }

    private Connection currentConn = null;

    @Override
    public  T withConnection(Function function) {
        try {
            if (currentConn != null) {
                return function.apply(currentConn);
            } else {
                try (Connection connection = dataSource.getConnection()) {
                    currentConn = connection;
                    return function.apply(connection);
                } finally {
                    currentConn = null;
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Object[] buildSql(SqlSignature signature, Object[] args, Function buildSqlClosure) {
        throw new IllegalStateException("method not implemented");
    }

    @Override
    public  T execute(SqlSignature signature, Object[] methodArgs, String sql, List sqlArgs, SqlExecutor executor, ResultHandler resultHandler) {
        throw new IllegalStateException("method not implemented");
    }
}