Example usage for org.springframework.jdbc.datasource SingleConnectionDataSource SingleConnectionDataSource

List of usage examples for org.springframework.jdbc.datasource SingleConnectionDataSource SingleConnectionDataSource

Introduction

In this page you can find the example usage for org.springframework.jdbc.datasource SingleConnectionDataSource SingleConnectionDataSource.

Prototype

public SingleConnectionDataSource(String url, String username, String password, boolean suppressClose) 

Source Link

Document

Create a new SingleConnectionDataSource with the given standard DriverManager parameters.

Usage

From source file:nz.geek.caffe.spring.hdb.HANAExceptionMappingTest.java

/**
 * init./*from  w  ww .  j  a v  a2 s.  c  o m*/
 */
@BeforeClass
public static void staticInit() {
    datasource = new SingleConnectionDataSource(System.getProperty("jdbcUrl", "jdbc:sap://localhost:30115"),
            System.getProperty("jdbcUser", "spring"), System.getProperty("jdbcPassword", "spring"), true);
}

From source file:org.openmrs.module.tribe.TribeActivator.java

private void extractTribeColumn() {
    // check whether tribe module is installed in an old OpenMRS
    // throw exception if it is
    boolean isOldOpenMRS = true;
    try {//  w  w  w . j  av a  2 s  .co  m
        Class cls = OpenmrsClassLoader.getInstance().loadClass("org.openmrs.Tribe");
        if (cls.isAnnotationPresent(Deprecated.class))
            isOldOpenMRS = false;
    } catch (ClassNotFoundException e) {
        // OpenMRS version ok
        isOldOpenMRS = false;
    }
    if (isOldOpenMRS) {
        throw new ModuleException(
                "Tribe module cannot be used with this OpenMRS version. Please upgrade OpenMRS.");
    }

    // now convert patient tribes to tribe attributes
    AdministrationService as = Context.getAdministrationService();

    // check whether patient table has tribe column
    log.info("Ignore the error message if occurs: "
            + "Error while running sql: SELECT distinct tribe from patient where 1 = 0");
    log.info("The above message indicates that you are running a new implementation "
            + "which does not have a tribe column in the patient table.");
    boolean isTribeColumnExists = true;

    DataSource ds = new SingleConnectionDataSource(Context.getRuntimeProperties().getProperty("connection.url"),
            Context.getRuntimeProperties().getProperty("connection.username"),
            Context.getRuntimeProperties().getProperty("connection.password"), true);
    JdbcTemplate jdbc = new JdbcTemplate(ds);

    jdbc.setMaxRows(1);
    try {

        jdbc.queryForList("SELECT distinct tribe from patient where 1 = 0");

    } catch (Exception e) {
        isTribeColumnExists = false;

    }

    // now convert patient tribes to tribe attributes

    if (isTribeColumnExists) {
        // create tribe attributes
        try {
            log.info("Transforming tribe details");
            Context.addProxyPrivilege(OpenmrsConstants.PRIV_SQL_LEVEL_ACCESS);
            as.executeSQL(
                    "INSERT INTO person_attribute (person_id, value, person_attribute_type_id, creator, date_created, uuid)"
                            + " SELECT patient_id, tribe,"
                            + " (SELECT person_attribute_type_id FROM person_attribute_type WHERE name = 'Tribe')"
                            + " , 1, now(), UUID() FROM patient WHERE tribe IS NOT NULL;",
                    false);
        } finally {
            Context.removeProxyPrivilege(OpenmrsConstants.PRIV_SQL_LEVEL_ACCESS);
        }

        // drop tribe column in patient, this will make these scripts not run again
        log.info("Dropping old tribe column");
        try {
            Context.addProxyPrivilege(OpenmrsConstants.PRIV_SQL_LEVEL_ACCESS);
            as.executeSQL("ALTER TABLE patient DROP FOREIGN KEY belongs_to_tribe;", false);
        } catch (Exception e) {
            log.warn("Unable to drop foreign key patient.belongs_to_tribe", e);
        } finally {
            Context.removeProxyPrivilege(OpenmrsConstants.PRIV_SQL_LEVEL_ACCESS);
        }

        try {
            Context.addProxyPrivilege(OpenmrsConstants.PRIV_SQL_LEVEL_ACCESS);
            as.executeSQL("ALTER TABLE patient DROP COLUMN tribe;", false);
        } catch (Exception e) {
            log.warn("Unable to drop column patient.tribe", e);
        } finally {
            Context.removeProxyPrivilege(OpenmrsConstants.PRIV_SQL_LEVEL_ACCESS);
        }

        log.info("Tribe data conversion complete");
    }

    // add View Tribes privilege to Authenticated role if not exists
    try {
        Context.addProxyPrivilege(OpenmrsConstants.PRIV_SQL_LEVEL_ACCESS);
        UserService us = Context.getUserService();
        Role authenticatedRole = us.getRole(OpenmrsConstants.AUTHENTICATED_ROLE);
        if (!authenticatedRole.hasPrivilege(TribeConstants.PRIV_VIEW_TRIBES)) {
            as.executeSQL("INSERT INTO role_privilege (role, privilege) VALUES ('"
                    + OpenmrsConstants.AUTHENTICATED_ROLE + "', '" + TribeConstants.PRIV_VIEW_TRIBES + "')",
                    false);
        }
    } finally {
        Context.removeProxyPrivilege(OpenmrsConstants.PRIV_SQL_LEVEL_ACCESS);
    }
}

From source file:ca.nrc.cadc.db.DBUtil.java

/**
 * Create a DataSource with a single connection to the server.  All failures
 * are thrown as RuntimeException with a Throwable (cause).
 *
 * @param config/*from w  w w.ja va  2  s  .c o m*/
 * @param suppressClose suppress close calls on the underlying Connection
 * @param test test the datasource before return (might throw)
 * @return a connected single connection DataSource
 * @throws DBConfigException
 */
public static DataSource getDataSource(ConnectionConfig config, boolean suppressClose, boolean test)
        throws DBConfigException {
    try {
        log.debug("server: " + config.getServer());
        log.debug("driver: " + config.getDriver());
        log.debug("url: " + config.getURL());
        log.debug("database: " + config.getDatabase());

        // load JDBC driver
        Class.forName(config.getDriver());

        SingleConnectionDataSource ds = new SingleConnectionDataSource(config.getURL(), config.getUsername(),
                config.getPassword(), suppressClose);

        Properties props = new Properties();
        props.setProperty("APPLICATIONNAME", getMainClass());
        ds.setConnectionProperties(props);

        if (test)
            testDS(ds, true);

        return ds;
    } catch (ClassNotFoundException ex) {
        throw new DBConfigException("failed to load JDBC driver: " + config.getDriver(), ex);
    } catch (SQLException ex) {
        throw new DBConfigException("failed to open connection: " + config.getURL(), ex);
    }

}

From source file:com.ms.commons.test.tool.ExportDatabaseData.java

private static JdbcTemplate getJdbcTemplate(DatabaseConfigItem item) {
    try {//w  ww. j a  v a2 s.c  o m
        Class.forName(item.getDriver());
        DataSource dataSource = new SingleConnectionDataSource(item.getUrl(), item.getUsername(),
                item.getPassword(), true);
        return new JdbcTemplate(dataSource);
    } catch (Exception e) {
        System.out.println("Error when get jdbc template: " + e.getMessage());
        return null;
    }
}

From source file:org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImplTests.java

@BeforeClass
public static void createDataSource() {
    dataSource = new SingleConnectionDataSource("jdbc:hsqldb:mem:tokenrepotest", "sa", "", true);
    dataSource.setDriverClassName("org.hsqldb.jdbc.JDBCDriver");
}