Example usage for java.sql Connection getAutoCommit

List of usage examples for java.sql Connection getAutoCommit

Introduction

In this page you can find the example usage for java.sql Connection getAutoCommit.

Prototype

boolean getAutoCommit() throws SQLException;

Source Link

Document

Retrieves the current auto-commit mode for this Connection object.

Usage

From source file:org.wso2.carbon.identity.application.mgt.dao.impl.ApplicationDAOImpl.java

/**
 *
 *///from  w ww .ja  v  a2  s  .  c  o  m

@Override
public void updateApplication(ServiceProvider serviceProvider) throws IdentityApplicationManagementException {

    Connection connection = IdentityDatabaseUtil.getDBConnection();
    int applicationId = serviceProvider.getApplicationID();

    try {
        if (ApplicationManagementServiceComponent.getFileBasedSPs()
                .containsKey(serviceProvider.getApplicationName())) {
            throw new IdentityApplicationManagementException(
                    "Application with the same name laoded from the file system.");
        }

        // update basic information of the application.
        // you can change application name, description, isSasApp...
        updateBasicApplicationData(applicationId, serviceProvider.getApplicationName(),
                serviceProvider.getDescription(), serviceProvider.isSaasApp(), connection);
        updateInboundProvisioningConfiguration(applicationId, serviceProvider.getInboundProvisioningConfig(),
                connection);

        // delete all in-bound authentication requests.
        deleteInboundAuthRequestConfiguration(serviceProvider.getApplicationID(), connection);

        // update all in-bound authentication requests.
        updateInboundAuthRequestConfiguration(serviceProvider.getApplicationID(),
                serviceProvider.getInboundAuthenticationConfig(), connection);

        // delete local and out-bound authentication configuration.
        deleteLocalAndOutboundAuthenticationConfiguration(applicationId, connection);

        // update local and out-bound authentication configuration.
        updateLocalAndOutboundAuthenticationConfiguration(serviceProvider.getApplicationID(),
                serviceProvider.getLocalAndOutBoundAuthenticationConfig(), connection);

        deleteRequestPathAuthenticators(applicationId, connection);
        updateRequestPathAuthenticators(applicationId, serviceProvider.getRequestPathAuthenticatorConfigs(),
                connection);

        deteClaimConfiguration(applicationId, connection);
        updateClaimConfiguration(serviceProvider.getApplicationID(), serviceProvider.getClaimConfig(),
                applicationId, connection);

        deleteOutboundProvisioningConfiguration(applicationId, connection);
        updateOutboundProvisioningConfiguration(applicationId, serviceProvider.getOutboundProvisioningConfig(),
                connection);

        deletePermissionAndRoleConfiguration(applicationId, connection);
        updatePermissionAndRoleConfiguration(serviceProvider.getApplicationID(),
                serviceProvider.getPermissionAndRoleConfig(), connection);
        deleteAssignedPermissions(connection, serviceProvider.getApplicationName(),
                serviceProvider.getPermissionAndRoleConfig().getPermissions());

        if (!connection.getAutoCommit()) {
            connection.commit();
        }
    } catch (SQLException | UserStoreException e) {
        try {
            if (connection != null) {
                connection.rollback();
            }
        } catch (SQLException e1) {
            throw new IdentityApplicationManagementException(
                    "Failed to update service provider " + applicationId, e);
        }
        throw new IdentityApplicationManagementException("Failed to update service provider " + applicationId,
                e);
    } finally {
        IdentityApplicationManagementUtil.closeConnection(connection);
    }
}

From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java

/**
 * Prepared the connection for metadata operations.
 *///from   w w w  .j  a  va2  s. c  o m
private void beforeMetadataOperation(Connection c) {
    if (requiresAutoCommitForMetaData) {
        try {
            c.rollback();
        } catch (SQLException sqle) {
        }
        try {
            if (!c.getAutoCommit())
                c.setAutoCommit(true);
        } catch (SQLException sqle) {
        }
    }
}

From source file:org.apache.roller.weblogger.business.startup.DatabaseInstaller.java

/**
 * Upgrade database for Roller 2.1.0/* w w w . ja  va  2  s  .  c  om*/
 */
private void upgradeTo210(Connection con, boolean runScripts) throws StartupException {
    SQLScriptRunner runner = null;
    try {
        if (runScripts) {
            String handle = getDatabaseHandle(con);
            String scriptPath = handle + "/200-to-210-migration.sql";
            successMessage("Running database upgrade script: " + scriptPath);
            runner = new SQLScriptRunner(scripts.getDatabaseScript(scriptPath));
            runner.runScript(con, true);
            messages.addAll(runner.getMessages());
        }

        /*
         * For Roller 2.1.0 we are going to standardize some of the
         * weblog templates and make them less editable.  To do this
         * we need to do a little surgery.
         *
         * The goal for this upgrade is to ensure that ALL weblogs now have
         * the required "Weblog" template as their default template.
         */

        successMessage("Doing upgrade to 210 ...");
        successMessage("Ensuring that all weblogs use the 'Weblog' template as their default page");

        // this query will give us all websites that have modified their
        // default page to link to something other than "Weblog"
        PreparedStatement selectUpdateWeblogs = con
                .prepareStatement("select website.id,template,website.handle from website,webpage "
                        + "where webpage.id = website.defaultpageid " + "and webpage.link != 'Weblog'");

        PreparedStatement selectWeblogTemplate = con
                .prepareStatement("select id from webpage where websiteid = ? and link = 'Weblog'");

        PreparedStatement updateWeblogTemplate = con
                .prepareStatement("update webpage set template = ? where id = ?");

        // insert a new template for a website
        PreparedStatement insertWeblogTemplate = con.prepareStatement("insert into webpage"
                + "(id, name, description, link, websiteid, template, updatetime) " + "values(?,?,?,?,?,?,?)");

        // update the default page for a website
        PreparedStatement updateDefaultPage = con
                .prepareStatement("update website set defaultpageid = ? " + "where id = ?");

        String description = "This template is used to render the main " + "page of your weblog.";
        ResultSet websiteSet = selectUpdateWeblogs.executeQuery();
        Date now = new Date();
        while (websiteSet.next()) {
            String websiteid = websiteSet.getString(1);
            String template = websiteSet.getString(2);
            String handle = websiteSet.getString(3);
            successMessage("Processing website: " + handle);

            String defaultpageid = null;

            // it's possible that this weblog has a "Weblog" template, but just
            // isn't using it as their default.  if so we need to fix that.
            selectWeblogTemplate.clearParameters();
            selectWeblogTemplate.setString(1, websiteid);
            ResultSet weblogPageSet = selectWeblogTemplate.executeQuery();
            if (weblogPageSet.next()) {
                // this person already has a "Weblog" template, so update it
                String id = weblogPageSet.getString(1);

                updateWeblogTemplate.clearParameters();
                updateWeblogTemplate.setString(1, template);
                updateWeblogTemplate.setString(2, id);
                updateWeblogTemplate.executeUpdate();

                // make sure and adjust what default page id we want to use
                defaultpageid = id;
            } else {
                // no "Weblog" template, so insert a new one
                insertWeblogTemplate.clearParameters();
                insertWeblogTemplate.setString(1, websiteid + "q");
                insertWeblogTemplate.setString(2, "Weblog");
                insertWeblogTemplate.setString(3, description);
                insertWeblogTemplate.setString(4, "Weblog");
                insertWeblogTemplate.setString(5, websiteid);
                insertWeblogTemplate.setString(6, template);
                insertWeblogTemplate.setDate(7, new java.sql.Date(now.getTime()));
                insertWeblogTemplate.executeUpdate();

                // set the new default page id
                defaultpageid = websiteid + "q";
            }

            // update defaultpageid value
            updateDefaultPage.clearParameters();
            updateDefaultPage.setString(1, defaultpageid);
            updateDefaultPage.setString(2, websiteid);
            updateDefaultPage.executeUpdate();
        }

        if (!con.getAutoCommit())
            con.commit();

        successMessage("Upgrade to 210 complete.");

    } catch (Exception e) {
        log.error("ERROR running 310 database upgrade script", e);
        if (runner != null)
            messages.addAll(runner.getMessages());

        log.error("Problem upgrading database to version 210", e);
        throw new StartupException("Problem upgrading database to version 210", e);
    }

    updateDatabaseVersion(con, 210);
}

From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java

/**
 * This method is called when the dictionary first sees any connection.
 * It is used to initialize dictionary metadata if needed. If you
 * override this method, be sure to call
 * <code>super.connectedConfiguration</code>.
 *///from  w ww .  j av a2 s  . c  o m
public void connectedConfiguration(Connection conn) throws SQLException {
    if (!connected) {
        DatabaseMetaData metaData = null;
        try {
            metaData = conn.getMetaData();

            databaseProductName = nullSafe(metaData.getDatabaseProductName());
            databaseProductVersion = nullSafe(metaData.getDatabaseProductVersion());
            setMajorVersion(metaData.getDatabaseMajorVersion());
            setMinorVersion(metaData.getDatabaseMinorVersion());
            try {
                // JDBC3-only method, so it might throw an
                // AbstractMethodError
                int JDBCMajorVersion = metaData.getJDBCMajorVersion();
                isJDBC3 = JDBCMajorVersion >= 3;
                isJDBC4 = JDBCMajorVersion >= 4;
            } catch (Throwable t) {
                // ignore if not JDBC3
            }
        } catch (Exception e) {
            if (log.isTraceEnabled())
                log.trace(e.toString(), e);
        }

        if (log.isTraceEnabled()) {
            log.trace(DBDictionaryFactory.toString(metaData));

            if (isJDBC3) {
                try {
                    log.trace(_loc.get("connection-defaults", new Object[] { conn.getAutoCommit(),
                            conn.getHoldability(), conn.getTransactionIsolation() }));
                } catch (Throwable t) {
                    log.trace("Unable to trace connection settings", t);
                }
            }
        }

        // Configure the naming utility
        if (supportsDelimitedIdentifiers == null) // not explicitly set
            configureNamingUtil(metaData);

        // Auto-detect generated keys retrieval support unless user specified it.
        if (supportsGetGeneratedKeys == null) {
            supportsGetGeneratedKeys = (isJDBC3) ? metaData.supportsGetGeneratedKeys() : false;
        }
        if (log.isInfoEnabled()) {
            log.info(_loc.get("dict-info", new Object[] { metaData.getDatabaseProductName(), getMajorVersion(),
                    getMinorVersion(), metaData.getDriverName(), metaData.getDriverVersion() }));
        }
    }
    connected = true;
}