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

📄 dbpoolmanager.java

📁 JSP移动商品管理平台源代码.........
💻 JAVA
字号:
package cmis.database;

import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
import java.util.Date;
import java.lang.InterruptedException;

public class DBPoolManager {

	private Context initCtx = null;
	private Context ctx = null;
	private DataSource ds = null;
	private long timeout = 5000;

	public Connection conn = null;

	// private Statement stmt = null;
	// private PreparedStatement pstmt = null;

	public DBPoolManager() {
		try {
			initCtx = new InitialContext();
			if (initCtx == null) {
				throw new Exception("Initial Failed!");
			}

			ctx = (Context) initCtx.lookup("java:comp/env");
			if (ctx != null) {
				ds = (DataSource) ctx.lookup("jdbc/SqlServerDB");
			}

			if (ds == null) {
				throw new Exception("Look up DataSource Failed!");
			}
		} catch (Exception ex) {
			log("Initial Exception: " + ex.toString());
		}
	}

	private void log(String str) {
		System.err.println(new Date() + " : " + str);
	}

	public synchronized void getConnection() {
		long startTime = new Date().getTime();
		while (conn == null) {
			newConnection();
			if (conn != null) {
				break;
			}

			try {
				wait(timeout);
			} catch (InterruptedException ex) {
				log("Get Connection Exception: " + ex.toString());
			}

			if ((new Date().getTime() - startTime) > timeout) {
				log("Connection TimeOut!");
				break;
			}
		}
	}

	private void newConnection() {
		try {
			conn = ds.getConnection();
			if (conn == null) {
				throw new Exception("Create Connection Failed!");
			}
		} catch (Exception ex) {
			log("Create Connection Exception: " + ex.toString());
		}
	}

	public void setAutoCommit(boolean autoCommit) {
		try {
			conn.setAutoCommit(autoCommit);
		} catch (Exception ex) {
			log("Set AutoCommit " + autoCommit + " Exception: " + ex.toString());
		}
	}

	public void commit() {
		try {
			conn.commit();
			setAutoCommit(true);
		} catch (Exception ex) {
			log("Commit Exception: " + ex.toString());
		}
	}

	public void rollback() {
		try {
			conn.rollback();
			setAutoCommit(true);
		} catch (Exception ex) {
			log("Rollback Exception: " + ex.toString());
		}
	}

	public synchronized void freeConnection() {
		try {
			if (conn != null) {
				conn.close();
				conn = null;
			}
		} catch (Exception ex) {
			log("Free Connection Exception: " + ex.toString());
		}
	}
}

⌨️ 快捷键说明

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