LMODTools.java [src/java/crlmod/utils] Revision:   Date:
/*
 * $Id: 1.0+65 LMODTools.java 402d39c37049 2021-12-29 od $
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API, and application suite.
 *
 * 2012-2024, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package crlmod.utils;

import csip.Config;
import csip.api.server.ServiceException;
import csip.SessionLogger;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;
import org.apache.tomcat.jdbc.pool.DataSourceProxy;
import org.apache.tomcat.jdbc.pool.PoolProperties;

/**
 *
 * @author LYaege
 */
@Deprecated
public class LMODTools {

    private static DataSource datasource;
    private static String connectionString;


    public static synchronized Connection getConnection(String className, SessionLogger logger) throws ServiceException {
        String confString = Config.getString("lmod.db", "");

        if (confString == null || confString.isEmpty()) {
            logger.log(Level.SEVERE, "Connection string not provided.  Please configure lmod.db configuration parameter.");
            throw new ServiceException("Unable to connect to LMOD.  Please check the lmod.db configuration parameter");
        }

        try {
            if (datasource == null || !confString.equals(connectionString)) {
                //Close previous datasource if it exists
                DataSourceProxy proxy = (DataSourceProxy) datasource;
                if (datasource != null) {
                    proxy.close();
                    datasource = null;
                }

                //Remember this connection string in case it's changed.
                connectionString = confString;

                PoolProperties p = new PoolProperties();
                p.setUrl(connectionString);
                p.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                p.setJmxEnabled(false);
                p.setTestOnBorrow(true);
                p.setValidationQuery("SELECT 1");
                p.setTestOnReturn(false);
                p.setValidationInterval(30000);
                p.setTimeBetweenEvictionRunsMillis(30000);
                p.setMaxWait(10000);
                p.setRemoveAbandonedTimeout(600);
                p.setRemoveAbandoned(true);
                datasource = new org.apache.tomcat.jdbc.pool.DataSource(p);
                logger.info("\nCreated datasource " + datasource.toString());
            }
            return datasource.getConnection();
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, null, ex);
            throw new ServiceException("Failed to connect to LMOD.  Please check the lmod.db configuration parameter");
        }
    }


    public static void shutdownDataSource() {
        if (datasource != null && datasource instanceof DataSourceProxy) {
            DataSourceProxy proxy = (DataSourceProxy) datasource;
            proxy.close(true);
            datasource = null;
            System.out.println("Closed datasource.");
        }
    }
}