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

com.landawn.abacus.spring.DBUtil Maven / Gradle / Ivy

There is a newer version: 1.10.1
Show newest version
/*
 * Copyright (c) 2018, Haiyang Li.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.landawn.abacus.spring;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.jdbc.datasource.DataSourceUtils;

import com.landawn.abacus.util.JdbcUtil;
import com.landawn.abacus.util.JdbcUtil.PreparedCallableQuery;
import com.landawn.abacus.util.JdbcUtil.PreparedQuery;
import com.landawn.abacus.util.Try;

/**
 * Integrated with Spring JDBC/Transaction.
 * 
 * @author HaiYang Li
 * 
 * @see {@link com.landawn.abacus.util.JdbcUtil}
 */
public class DBUtil {
    private final DataSource ds;

    public DBUtil(DataSource ds) {
        this.ds = ds;
    }

    public PreparedQuery prepareQuery(final String sql) throws SQLException {
        final Connection conn = DataSourceUtils.getConnection(ds);
        PreparedQuery result = null;

        try {
            result = JdbcUtil.prepareQuery(conn, sql).onClose(createCloseHandler(conn));
        } finally {
            if (result == null && conn != null) {
                DataSourceUtils.releaseConnection(conn, ds);
            }
        }

        return result;
    }

    public PreparedQuery prepareQuery(final String sql, final boolean autoGeneratedKeys) throws SQLException {
        final Connection conn = DataSourceUtils.getConnection(ds);
        PreparedQuery result = null;

        try {
            result = JdbcUtil.prepareQuery(conn, sql, autoGeneratedKeys).onClose(createCloseHandler(conn));
        } finally {
            if (result == null && conn != null) {
                DataSourceUtils.releaseConnection(conn, ds);
            }
        }

        return result;
    }

    /** 
     *  
     * @param stmtCreator the created {@code PreparedStatement} will be closed after any execution methods in {@code PreparedQuery/PreparedCallableQuery} is called.
     * An execution method is a method which will trigger the backed {@code PreparedStatement/CallableStatement} to be executed, for example: get/query/queryForInt/Long/../findFirst/list/execute/....
     * @return
     * @throws SQLException
     */
    public PreparedQuery prepareQuery(final Try.Function stmtCreator) throws SQLException {
        final Connection conn = DataSourceUtils.getConnection(ds);
        PreparedQuery result = null;

        try {
            result = JdbcUtil.prepareQuery(conn, stmtCreator).onClose(createCloseHandler(conn));
        } finally {
            if (result == null && conn != null) {
                DataSourceUtils.releaseConnection(conn, ds);
            }
        }

        return result;
    }

    public PreparedCallableQuery prepareCallableQuery(final String sql) throws SQLException {
        final Connection conn = DataSourceUtils.getConnection(ds);
        PreparedCallableQuery result = null;

        try {
            result = JdbcUtil.prepareCallableQuery(conn, sql).onClose(createCloseHandler(conn));
        } finally {
            if (result == null && conn != null) {
                DataSourceUtils.releaseConnection(conn, ds);
            }
        }

        return result;
    }

    /** 
     * @param stmtCreator the created {@code CallableStatement} will be closed after any execution methods in {@code PreparedQuery/PreparedCallableQuery} is called.
     * An execution method is a method which will trigger the backed {@code PreparedStatement/CallableStatement} to be executed, for example: get/query/queryForInt/Long/../findFirst/list/execute/....
     * @return
     * @throws SQLException
     */
    public PreparedCallableQuery prepareCallableQuery(final Try.Function stmtCreator) throws SQLException {
        final Connection conn = DataSourceUtils.getConnection(ds);
        PreparedCallableQuery result = null;

        try {
            result = JdbcUtil.prepareCallableQuery(conn, stmtCreator).onClose(createCloseHandler(conn));
        } finally {
            if (result == null && conn != null) {
                DataSourceUtils.releaseConnection(conn, ds);
            }
        }

        return result;
    }

    private Runnable createCloseHandler(final Connection conn) {
        return new Runnable() {
            @Override
            public void run() {
                DataSourceUtils.releaseConnection(conn, ds);
            }
        };
    }

    public static interface Dao extends JdbcUtil.Dao {

        @Override
        default PreparedQuery prepareQuery(final DataSource ds, final String query) throws SQLException {
            final Connection conn = DataSourceUtils.getConnection(ds);
            PreparedQuery result = null;

            try {
                result = JdbcUtil.prepareQuery(conn, query).onClose(() -> DataSourceUtils.releaseConnection(conn, ds));
            } finally {
                if (result == null && conn != null) {
                    DataSourceUtils.releaseConnection(conn, ds);
                }
            }

            return result;
        }

        @Override
        default PreparedQuery prepareQuery(final DataSource ds, final String query, final boolean generateKeys) throws SQLException {
            final Connection conn = DataSourceUtils.getConnection(ds);
            PreparedQuery result = null;

            try {
                result = JdbcUtil.prepareQuery(conn, query, generateKeys).onClose(() -> DataSourceUtils.releaseConnection(conn, ds));
            } finally {
                if (result == null && conn != null) {
                    DataSourceUtils.releaseConnection(conn, ds);
                }
            }

            return result;
        }

        @Override
        default PreparedCallableQuery prepareCallableQuery(final DataSource ds, final String query) throws SQLException {
            final Connection conn = DataSourceUtils.getConnection(ds);
            PreparedCallableQuery result = null;

            try {
                result = JdbcUtil.prepareCallableQuery(conn, query).onClose(() -> DataSourceUtils.releaseConnection(conn, ds));
            } finally {
                if (result == null && conn != null) {
                    DataSourceUtils.releaseConnection(conn, ds);
                }
            }

            return result;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy