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

org.kawanfw.sql.servlet.connection.TransactionUtil Maven / Gradle / Ivy

/*
 * This file is part of AceQL. 
 * AceQL: Remote JDBC access over HTTP.                                     
 * Copyright (C) 2015,  KawanSoft SAS
 * (http://www.kawansoft.com). All rights reserved.                                
 *                                                                               
 * AceQL is free software; you can redistribute it and/or                 
 * modify it under the terms of the GNU Lesser General Public                    
 * License as published by the Free Software Foundation; either                  
 * version 2.1 of the License, or (at your option) any later version.            
 *                                                                               
 * AceQL is distributed in the hope that it will be useful,               
 * but WITHOUT ANY WARRANTY; without even the implied warranty of                
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             
 * Lesser General Public License for more details.                               
 *                                                                               
 * You should have received a copy of the GNU Lesser General Public              
 * License along with this library; if not, write to the Free Software           
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
 * 02110-1301  USA
 *
 * Any modifications to this file must keep this entire header
 * intact.
 */
package org.kawanfw.sql.servlet.connection;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;

import javax.servlet.http.HttpServletRequest;

import org.kawanfw.commons.api.server.CommonsConfigurator;
import org.kawanfw.commons.server.util.ServerLogger;
import org.kawanfw.commons.util.FrameworkDebug;
import org.kawanfw.commons.util.TransferStatus;
import org.kawanfw.file.util.parms.Parameter;
import org.kawanfw.sql.api.server.SqlConfigurator;
import org.kawanfw.sql.servlet.ConnectionCloser;
import org.kawanfw.sql.util.ConnectionParms;
import org.kawanfw.sql.util.SqlActionTransaction;
import org.kawanfw.sql.util.SqlReturnCode;

/**
 * @author Nicolas de Pomereu
 *
 *         Wrapper for transactions commands to decrease code in
 *         ServerSqlDispatch
 */
public class TransactionUtil {

    private static boolean DEBUG = FrameworkDebug.isSet(TransactionUtil.class);

    /**
     * Calls a commit(), rollback() or close() on a Connection
     * 
     * @param request
     * @param commonsConfigurator
     * @param sqlConfigurator
     * @param out
     * @param action
     * @throws IOException
     * @throws SQLException
     * @throws IllegalArgumentException
     */
    public static void setCommitRollbackCloseExecute(
	    HttpServletRequest request,
	    CommonsConfigurator commonsConfigurator,
	    SqlConfigurator sqlConfigurator, PrintWriter out, String action)
	    throws IOException, SQLException, IllegalArgumentException {
	String connectionId = request
		.getParameter(ConnectionParms.CONNECTION_ID);
	String username = request.getParameter(Parameter.USERNAME);

	ConnectionStore connectionStore = new ConnectionStore(username,
		connectionId);
	Connection connection = connectionStore.get();

	if (connection == null) {
	    out.println(TransferStatus.SEND_OK);
	    out.println(SqlReturnCode.SESSION_INVALIDATED);
	    return;
	}

	if (action.equals(SqlActionTransaction.ACTION_SQL_COMMIT)) {
	    connection.commit();
	} else if (action.equals(SqlActionTransaction.ACTION_SQL_ROLLBACK)) {
	    connection.rollback();
	} else if (action.equals(SqlActionTransaction.ACTION_SQL_CON_CLOSE)) {

	    debug("close asked from PC...");
	    connectionStore.remove();

	    ConnectionCloser.freeConnection(connection, sqlConfigurator);
	}

	out.println(TransferStatus.SEND_OK);

    }

    /**
     * Calls a setAutocommit, setReadOnly(), setHoldbility,
     * getTransactionIsolation
     * 
     * @param request
     * @param commonsConfigurator
     * @param out
     * @param action
     * @throws IOException
     * @throws SQLException
     * @throws IllegalArgumentException
     */
    public static void setAutocommitReadOnlyHoldabilityTransactionInsolationExecute(
	    HttpServletRequest request,
	    CommonsConfigurator commonsConfigurator, PrintWriter out,
	    String action) throws IOException, SQLException,
	    IllegalArgumentException {
	String connectionId = request
		.getParameter(ConnectionParms.CONNECTION_ID);
	String username = request.getParameter(Parameter.USERNAME);

	ConnectionStore connectionStore = new ConnectionStore(username,
		connectionId);
	Connection connection = connectionStore.get();

	if (connection == null) {
	    out.println(TransferStatus.SEND_OK);
	    out.println(SqlReturnCode.SESSION_INVALIDATED);
	    return;
	}

	if (action.equals(SqlActionTransaction.ACTION_SQL_SET_AUTOCOMMIT)) {
	    boolean autoCommit = Boolean.parseBoolean(request
		    .getParameter(ConnectionParms.AUTOCOMMIT));
	    connection.setAutoCommit(autoCommit);
	} else if (action.equals(SqlActionTransaction.ACTION_SQL_SET_READ_ONLY)) {
	    boolean readOnly = Boolean.parseBoolean(request
		    .getParameter(ConnectionParms.READONLY));
	    connection.setReadOnly(readOnly);
	} else if (action
		.equals(SqlActionTransaction.ACTION_SQL_SET_HOLDABILITY)) {
	    int holdability = Integer.parseInt(request
		    .getParameter(ConnectionParms.HOLDABILITY));
	    connection.setHoldability(holdability);
	} else if (action
		.equals(SqlActionTransaction.ACTION_SQL_SET_TRANSACTION_ISOLATION)) {
	    int level = Integer.parseInt(request
		    .getParameter(ConnectionParms.TRANSACTION_ISOLATION));
	    connection.setTransactionIsolation(level);
	} else {
	    throw new IllegalArgumentException(
		    "Invalid Sql Action for setAutocommitReadOnlyHoldabilityTransactionInsolationExecute(): "
			    + action);
	}

	out.println(TransferStatus.SEND_OK);

    }

    /**
     * Calls a getAutocommit, isReadOnly(), getHoldbility,
     * SetTransactionIsolation
     * 
     * @param request
     * @param commonsConfigurator
     * @param out
     * @param action
     * @throws IOException
     * @throws SQLException
     * @throws IllegalArgumentException
     */

    public static void getAutocommitReadOnlyHoldabilityExecute(
	    HttpServletRequest request,
	    CommonsConfigurator commonsConfigurator, PrintWriter out,
	    String action) throws IOException, SQLException,
	    IllegalArgumentException {

	String connectionId = request
		.getParameter(ConnectionParms.CONNECTION_ID);
	String username = request.getParameter(Parameter.USERNAME);

	ConnectionStore connectionStore = new ConnectionStore(username,
		connectionId);
	Connection connection = connectionStore.get();

	if (connection == null) {
	    out.println(TransferStatus.SEND_OK);
	    out.println(SqlReturnCode.SESSION_INVALIDATED);
	    return;
	}

	if (action.equals(SqlActionTransaction.ACTION_SQL_GET_AUTOCOMMIT)) {
	    boolean autoCommit = connection.getAutoCommit();
	    out.println(TransferStatus.SEND_OK);
	    out.println(autoCommit + "");
	} else if (action.equals(SqlActionTransaction.ACTION_SQL_IS_READ_ONLY)) {
	    boolean readOnly = connection.isReadOnly();
	    out.println(TransferStatus.SEND_OK);
	    out.println(readOnly + "");
	} else if (action
		.equals(SqlActionTransaction.ACTION_SQL_GET_HOLDABILITY)) {
	    int holdability = connection.getHoldability();
	    out.println(TransferStatus.SEND_OK);
	    out.println(holdability + "");
	} else {
	    throw new IllegalArgumentException(
		    "Invalid Sql Action for setAutocommitReadOnlyHoldabilityTransactionInsolationExecute(): "
			    + action);
	}
    }

    /**
     * Method called by children Servlet for debug purpose println is done only
     * if class name name is in kawansoft-debug.ini
     */
    public static void debug(String s) {
	if (DEBUG) {
	    ServerLogger.getLogger().log(Level.WARNING, s);
	}
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy