⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wsbsdatasource.java

📁 获取WebLogic中定义的数据源的源代码
💻 JAVA
字号:
package inspur.tax.wsbs.htcl.database;

import java.io.*;
import java.util.*;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;

public class WsbsDataSource {
	private static Log log = LogFactory.getLog(WsbsDataSource.class);

	private static Map dataSourceMap = Collections
			.synchronizedMap(new HashMap());// 数据源缓冲区

	private static DataBaseUtil db = null;// 内部数据库实用类的静态对象

	public WsbsDataSource() {
		if (db == null) {
			db = new DataBaseUtil();
		}
	}

	public DataSource getDataSource() {
		return db.getDataSource();
	}

	public Connection getConnection() {
		return db.getConnection();
	}

	private static class DataBaseUtil {
		private final static String fileName = "wsbs_jdbcSupport.properties";

		private static boolean flag = false;// 用于标记是否需要读数据库配置文件

		private static Properties prop = null;// 属性,用于放数据库配置文件中的值

		public DataSource getDataSource() {
			String dataSourceName = "";
			try {
				dataSourceName = getDataSourceString();
				DataSource dataSource = (DataSource) dataSourceMap
						.get(dataSourceName);
				if (null != dataSource) {
					return dataSource;
				}
				Context context = new InitialContext();
				if (context == null) {
					log.error("Context为空!");
					throw new RuntimeException("Context为空!");
				}
				dataSource = (DataSource) context.lookup(dataSourceName);
				dataSourceMap.put(dataSourceName, dataSource);
				return dataSource;
			} catch (Exception ex) {
				log.error("查找名为" + dataSourceName + "的数据源失败");
				ex.printStackTrace();
				throw new RuntimeException();
			}
		}

		public Connection getConnection() {
			Connection conn;
			try {
				conn = getDataSource().getConnection();
			} catch (SQLException sqlEx) {
				throw new RuntimeException(sqlEx);
			}
			return conn;
		}

		private String getDataSourceString() {
			return getProperty("wsbsDataSource");
		}

		private String getProperty(String key) {
			loadPropFile();
			return prop.getProperty(key);
		}

		private void loadPropFile() {
			try {
				if (!flag) {
					prop = new Properties();
					InputStream in = this.getClass().getResourceAsStream(
							fileName);
					prop.load(in);
					if (in != null) {
						in.close();
					}
					flag = true;
				}
			} catch (Exception e) {
				throw new RuntimeException("读数据源配置文件时失败!");
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -