设为首页 收藏本站
查看: 981|回复: 0

[经验分享] 使用weblogic连接池来得到数据库连接(通过配置文件进行读取的优化方案)

[复制链接]

尚未签到

发表于 2017-2-18 10:41:23 | 显示全部楼层 |阅读模式
  package com.etong.system;
  import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
  import javax.naming.Context;
import javax.naming.InitialContext;
  import com.etong.hr.DatabaseHRFactory;
import com.etong.hr.HRFactory;
import com.etong.statetaxbureau.workflow.BureauFactory;
import com.etong.workflow.DatabaseWorkflowFactory;
import com.etong.workflow.WorkflowFactory;
import com.roger.database.DBPersistence;
  /**
* <p>Title: </p>
* <p>Description: 资源工厂类, 用来产生系统需要用到的资源, 如数据库的连接</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author
* @version 1.0
*/
  public class ResourceFactory {
private static LogRelated lg = new LogRelated("D:/log","lgConnOracle");
private static int no = 0;
private static InitialContext JNDI_Context = null;
private static final java.sql.Connection getSQLServerConnection() {
  java.sql.Connection con = null;
try {
Context ctx = getInitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)
ctx.lookup("SQLDS");
con = ds.getConnection();
if (con == null) {
throw new WorkflowConnectionException("SQL数据库连接不上");
}
}
catch (Exception e) {
e.printStackTrace();
throw new WorkflowConnectionException("SQL数据库连接不上");
}
return con;
  }
  private static synchronized final java.sql.Connection getOracleConnection() {
  
java.sql.Connection con = null;
try {
if(no >= Integer.MAX_VALUE){
no = 0;
}
no++;
lg.log("no = " + no +";上下文初始前!---Context");
Context ctx = getInitialContext();
lg.log("no = " + no +";获得数据源前!----DataSource");

javax.sql.DataSource ds = (javax.sql.DataSource)
ctx.lookup("workflowDS");
lg.log("no = " + no +";获得连接前!---getConnection");
con = ds.getConnection();
lg.log("no = " + no +";获得连接后!---conn");
if (con == null) {
lg.log("no = " + no +";获得连接为空!");
throw new WorkflowConnectionException("工作流数据库连接不上");
}else{
lg.log("no = " + no + ";数据库连接成功!");
}
}
catch (Exception e) {
e.printStackTrace();
e.printStackTrace(lg.getLogPW());
throw new WorkflowConnectionException("工作流数据库连接不上");
}
return con;
  }
  private static Context getInitialContext() throws Exception {
//采用静态JNDI_Context来第一次读取上下文信息
if(JNDI_Context != null){
return JNDI_Context;
}
lg.log("no = " + no +";getInitialContext!---getInitialContext");
  Properties pro = new Properties();

FileInputStream fileInputStream = new FileInputStream(new File("C:/pro.properties"));
pro.load(fileInputStream);
String url = pro.getProperty("url");


String user = pro.getProperty("username");
String password = pro.getProperty("password");

fileInputStream.close();
  //结束
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS,
password == null ? "" : password);
}
JNDI_Context = new InitialContext(properties);
return JNDI_Context;
  }
  public static final java.sql.Connection getCTAISConnection() {
java.sql.Connection con = null;
  try {
Context ctx = getInitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)
ctx.lookup("ctaisDS");
con = ds.getConnection();
if (con == null) {
throw new CtaisConnectionException("CTAIS数据库连接不上");
}
}
catch (Exception e) {
throw new CtaisConnectionException("CTAIS数据库连接不上");
}
return con;
}
  public static final java.sql.Connection getConnection() {
if (isSQLServer()) {
return getSQLServerConnection();
}
else if
(isOracle()) {
return getOracleConnection();
}
else {
throw new WorkflowConnectionException("工作流数据库连接不上");
}
  }
  public static final DBPersistence createDBPersistence() {
DBPersistence db = new DBPersistence();
if (isSQLServer()) {
db.setConnection(getSQLServerConnection());
}
else if
(isOracle()) {
db.setConnection(getOracleConnection());
}
else {
throw new WorkflowConnectionException("工作流数据库连接不上");
}
return db;
}
  public static final DBPersistence createCTAISPersistence() {
DBPersistence db = new DBPersistence();
if (isSQLServer()) {
db.setConnection(getSQLServerConnection());
}
else if
(isOracle()) {
db.setConnection(getCTAISConnection());
}
else {
throw new CtaisConnectionException("CTAIS数据库连接不上");
}
return db;
  }
  /**
* 创建一个人事工厂类
* @return 人事工厂类
*/
public static final HRFactory createHRFactory() {
return new DatabaseHRFactory();
}
  /**
* 创建一个工作流工厂类
* @return 工作流工厂类
*/
public static final WorkflowFactory createWorkflowFactory() {
return new DatabaseWorkflowFactory();
}
  /**
* 创建一个系统工厂类
* @return 系统工厂类
*/
public static final SystemFactory createSystemFactory() {
if (isSQLServer()) {
return new SQLServerSystemFactory();
}
else if
(isOracle()) {
return new OracleSystemFactory();
}
else {
throw new IllegalArgumentException("工作流数据库连接不上");
}
  }
  public static final BureauFactory createBureauFactory() {
return new BureauFactory();
}
  /**
* 得到用户自定义的字段类型
* @return 字段类型
*/
public static final String[] getUserField() {
String[] s = {
"整型", "小数", "字符型", "日期型"};
return s;
}
  public static final boolean isSQLServer() {
return false;
}
  public static final boolean isOracle() {
return true;
}
  public static void main(String[] args) throws Exception {
}
}
  其中:LogRelated只是为了便于找问题,创建的一个日志文件而已.其源代码为.
  package com.etong.system;
  import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
import java.io.PrintWriter;
  public class LogRelated {
private String logDirName = "";
private String logFileName = "";
  private PrintWriter logPW = null;
  private String strFileTime = new java.sql.Timestamp(System.currentTimeMillis()).toString().substring(0, 10);
  public LogRelated(String logDirName, String logFileName) {
initialLog(logDirName, logFileName);
}
  private void initialLog(String logDirName, String logFileName) {
this.logDirName = logDirName;
this.logFileName = logFileName;
if (logDirName.indexOf("\\") != -1) {
System.out.println("Input logDirName is incorrect!Please input like UNIX Directory!");
return;
}
File logFile = null;
if (logDirName == null || logDirName.equals("")) {
logFile = new File(logFileName + "_" + this.strFileTime + ".txt");
} else {
logFile = new File(logDirName + "/" + logFileName + "_" + this.strFileTime + ".txt");
}
if (!logFile.isFile()) {
logFile = logFile.getAbsoluteFile();
File parentDir = new File(logFile.getParent());
if (!parentDir.exists()) {
parentDir.mkdirs();
}
}
try {
logPW = new PrintWriter(new FileWriter(logFile, true), true);
} catch (IOException e) {
System.err.println("Cann't open log file: " + logFile);
logPW = new PrintWriter(System.err);
}
  }
  public void log(String logContent) {
if (!new java.sql.Timestamp(System.currentTimeMillis()).toString().substring(0, 10).equals(this.strFileTime)) {
this.strFileTime = new java.sql.Timestamp(System.currentTimeMillis()).toString().substring(0, 10);
initialLog(this.logDirName, this.logFileName);
}
try {
logPW.println(new java.sql.Timestamp(System.currentTimeMillis()) + ": " + logContent);
} catch (Exception ex) {
this.initialLog(this.logDirName, this.logFileName);
ex.printStackTrace(logPW);
this.log(logContent);
}
}
  public String getLogFileName() {
return logFileName;
}
  public void setLogDirName(String logDirName) {
this.logDirName = logDirName;
}
  public void setLogFileName(String logFileName) {
this.logFileName = logFileName;
}
  public void setLogPW(PrintWriter logPW) {
this.logPW = logPW;
}
  public String getLogDirName() {
return logDirName;
}
  public PrintWriter getLogPW() {
return logPW;
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-343780-1-1.html 上篇帖子: weblogic部署报错request "1,435,307,455,967". Underlying error is: "null" 下篇帖子: 客户端查看缩略图,把html文件放到weblogic服务器上,在访问,就不能正常显示缩略图,
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表