Java Utililty Methods DataSource

List of utility methods to do DataSource

Description

The list of methods to do DataSource are organized into topic(s).

Method

ConnectiongetConnection(final DataSource ds, final boolean autoCommit)
get Connection
final Connection conn = ds.getConnection();
if (conn.getAutoCommit() != autoCommit) {
    conn.setAutoCommit(autoCommit);
return conn;
ConnectiongetConnectionFromDataSource(DataSource ds)
Creates a connection to the database from a Data Source
Connection conn = ds.getConnection();
conn.setAutoCommit(false);
return conn;
StringgetDatabaseName(DataSource dataSource)
get Database Name
Connection con = null;
try {
    con = dataSource.getConnection();
    if (con == null) {
        throw new RuntimeException("Connection returned by DataSource [" + dataSource + "] was null");
    DatabaseMetaData metaData = con.getMetaData();
    if (metaData == null) {
...
DataSourcegetDataSource(String jndi)
get Data Source
javax.naming.Context initContext;
try {
    initContext = new javax.naming.InitialContext();
    return (DataSource) initContext.lookup("java:comp/env/" + jndi);
} catch (Exception ex) {
    try {
        initContext = new javax.naming.InitialContext();
        return (DataSource) initContext.lookup(jndi);
...
DataSourcegetDataSource(String jndiName)
get Data Source
Context ctx = new InitialContext();
if (ctx == null) {
    throw new Exception("No Context available");
return (DataSource) ctx.lookup(jndiName);
DataSourcegetDataSource(String service)
get Data Source
Context ctx;
try {
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "org.mortbay.naming.InitialContextFactory");
    env.put(Context.PROVIDER_URL, "localhost:1099");
    ctx = new InitialContext(env);
} catch (Exception e) {
    logger.severe("Error configuring initial context " + e);
...
DataSourcegetDataSourceCache(String hostName)
get Data Source Cache
return dataSourceMap.get(hostName);
ConnectiongetDBConnection(DataSource dataSource)
get DB Connection
Connection dbConnection = dataSource.getConnection();
dbConnection.setAutoCommit(false);
if (dbConnection.getTransactionIsolation() != Connection.TRANSACTION_READ_COMMITTED) {
    dbConnection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
return dbConnection;
StringgetGlobalVariable(DataSource dataSource, String variable)
Get global variable value.
try (Connection conn = dataSource.getConnection()) {
    try (Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(String.format("show global variables like '%s'", variable));) {
        if (rs.next()) {
            return rs.getString(2);
        } else {
            return "";
StringgetJdbcUrlFromDataSource(DataSource dataSource)
get Jdbc Url From Data Source
Connection connection = null;
try {
    connection = dataSource.getConnection();
    if (connection == null) {
        throw new IllegalStateException("Connection returned by DataSource [" + dataSource + "] was null");
    return connection.getMetaData().getURL();
} catch (SQLException e) {
...