com.github.ddth.dao.jdbc.DbcHelper Maven / Gradle / Ivy
package com.github.ddth.dao.jdbc;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;
import javax.sql.DataSource;
/**
* Database Connectivity Helper class.
*
* @author Thanh Nguyen
* @since 0.2.0
*/
public class DbcHelper {
private final static ConcurrentMap jdbcDataSources = new ConcurrentHashMap();
public static void init() {
jdbcDataSources.clear();
}
public static void destroy() {
jdbcDataSources.clear();
}
/**
* Registers a named JDBC datasource.
*
* @param name
* @param dataSource
* @return
*/
public static boolean registerJdbcDataSource(String name, DataSource dataSource) {
return jdbcDataSources.putIfAbsent(name, dataSource) == null;
}
/**
* Unregisters a JDBC datasource by name.
*
* @param name
* @return
*/
public static boolean unregisterJdbcDataSource(String name) {
return jdbcDataSources.remove(name) != null;
}
/**
* Retrieves a JDBC datasource by name.
*
* @param name
* @return
*/
public static DataSource getJdbcDataSource(String name) {
return jdbcDataSources.get(name);
}
/*----------------------------------------------------------------------*/
private static class OpenConnStats {
// public String dsName;
public Connection conn;
public AtomicLong counter = new AtomicLong();
public boolean inTransaction = false;
}
private static ThreadLocal
© 2015 - 2025 Weber Informatics LLC | Privacy Policy