---
title: "tomcat websphere 从数据源中获取链接"
author: "wunda"
date: "2011-05-17T16:27:32Z"
tags: []
categories: ["企业架构"]
slug: "tomcat-websphere"
draft: false
lastmod: "2011-05-17T16:27:32Z"
id: 1047211
url: "https://wunda.iteye.com/blog/1047211"
---

public boolean getConnection() {

		/**
		 * 连接池直接配置tomcat
		 */
		try {
			Context initCtx = new InitialContext();
			if (initCtx == null)
				throw new Exception("没有匹配的上下文环境");
			
			// tomcat 的连接方式。
			Context envCtx = (Context) initCtx.lookup("java:comp/env");
			DataSource ds = (DataSource) envCtx.lookup("jdbc/trms");
			// websphere 的连接方式。
//			 System.out.println("数据库链接前");
//			DataSource ds = (DataSource) initCtx.lookup("jdbc/trms");
//           System.out.println("数据库链接成功");
			if (ds == null)
				throw new Exception("没有匹配的数据库");
			conn = ds.getConnection();
			conn.setAutoCommit(true);
			// conn=DriverManager.getConnection("proxool.connpool");
			return true;
		} catch (Exception e) {
			// 系统异常记录日志
			error = "《＝＝tool/com.cncsi.dm.db.conn/dbConnect//数据库公共类处理异常＝＝》";
			UserException.error(error);
			UserException.error(e.toString());
			e.printStackTrace();
			return false;
		}
	}