JAVA 数据库切换 -1 (MYSQL 连接)后续加锁
public class SqlMyDataSource implements DataSource{ private static SqlMyDataSource instance = new SqlMyDataSource(); private SqlMyDataSource() { } public static SqlMyDataSource getInstance() { return instance; } private static LinkedList<Connection> pool = new LinkedList<Connection>(); private static String DriverName = "com.mysql.jdbc.Driver"; private static String url = ""; private static String username = ""; private static String password = ""; private static Connection conn = null; private static Logger logger = Logger.getLogger(SqlMyDataSource.class); static{ try { ResourceBundle res = ResourceBundle.getBundle("uh"); url = res.getString("mysql.url"); username = res.getString("mysql.username"); password = res.getString("mysql.password"); DriverName = res.getString("mysql.driver"); Class.forName(DriverName); conn = DriverManager.getConnection(url, username, password); logger.info("mysql connect successfully"); } catch (Exception e) { logger.info("mysql connect fail"); e.printStackTrace(); throw new RuntimeException("mysql connect fail"); } } @Override public Connection getConnection() throws SQLException { if(conn == null || conn.isClosed()) { conn = DriverManager.getConnection(url, username, password); } return conn; } @Override public PrintWriter getLogWriter() throws SQLException { // TODO Auto-generated method stub return null; } @Override public void setLogWriter(PrintWriter out) throws SQLException { // TODO Auto-generated method stub } @Override public void setLoginTimeout(int seconds) throws SQLException { // TODO Auto-generated method stub } @Override public int getLoginTimeout() throws SQLException { // TODO Auto-generated method stub return 0; } @Override public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException { // TODO Auto-generated method stub return null; } @Override public <T> T unwrap(Class<T> iface) throws SQLException { // TODO Auto-generated method stub return null; } @Override public boolean isWrapperFor(Class<?> iface) throws SQLException { // TODO Auto-generated method stub return false; } @Override public Connection getConnection(String username, String password) throws SQLException { // TODO Auto-generated method stub return null; } }