/*
Copyright (C) 2000-2010  Ministere de la culture et de la communication (France), AJLSM
See LICENCE file
*/
package fr.gouv.culture.sdx.utils.database;

import fr.gouv.culture.sdx.exception.SDXException;
import fr.gouv.culture.sdx.exception.SDXExceptionCode;
import fr.gouv.culture.sdx.utils.AbstractSdxObject;

import java.sql.Connection;
import java.sql.SQLException;

public class SQLDatabaseConnection extends AbstractSdxObject implements DatabaseConnection {
    Connection conn = null;

    public SQLDatabaseConnection() {
    }

    public SQLDatabaseConnection(Connection conn) {
        this.conn = conn;
    }

    public void commit() throws SDXException {
        try {
            if (this.conn != null)
                this.conn.commit();
        } catch (SQLException e) {
            throw new SDXException(super.getLog(), SDXExceptionCode.ERROR_COMMIT_SQL_CONNECTION, null, e);
        }
    }

    public void rollback() throws SDXException {
        try {
            if (this.conn != null)
                this.conn.rollback();
        } catch (SQLException e) {
            throw new SDXException(super.getLog(), SDXExceptionCode.ERROR_ROLLBACK_SQL_CONNECTION, null, e);
        }
    }

    public void setAutoCommit(boolean autoCommit) throws SDXException {
        try {
            if (this.conn != null)
                this.conn.setAutoCommit(autoCommit);
        } catch (SQLException e) {
            throw new SDXException(super.getLog(), SDXExceptionCode.ERROR_SET_AUTO_COMMIT_SQL_CONNECTION, null, e);
        }
    }

    /**
     * Returns the SQL connection to the datasource component
     */
    public Connection getConnection() {
        return this.conn;
    }

    public void close() throws SDXException {
        try {
            if (this.conn != null)
                this.conn.close();
        } catch (SQLException e) {
            throw new SDXException(super.getLog(), SDXExceptionCode.ERROR_COMMIT_SQL_CONNECTION, null, e);
        }
    }

    public void optimize() throws SDXException {
        //do nothing here
    }

    protected String getClassNameSuffix() {
        return DatabaseConnection.CLASS_NAME_SUFFIX;
    }

	/* (non-Javadoc)
	 * @see fr.gouv.culture.sdx.utils.AbstractSdxObject#initToSax()
	 */
	protected boolean initToSax() {
		return true;
	}

	/**Init the LinkedHashMap _xmlizable_volatile_objects with the objects in order to describ them in XML
	 * Some objects need to be refresh each time a toSAX is called*/
	protected void initVolatileObjectsToSax() {

	}


}
