📄 dbconnectionmanager.java
字号:
/*
Loader - tool for transfering data from one JDBC source to another and
doing transformations during copy.
Copyright (C) 2002-2003 Together
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Loader.java
Date: 03.03.2003.
@version 2.1 alpha
@authors:
Milosevic Sinisa sinisa@prozone.yu
Radoslav Dutina rale@prozone.co.yu
*/
package org.webdocwf.util.loader;
import java.io.*;
import java.util.*;
import java.sql.*;
/**
* DBConnectionManager class creates all connection to database.
* @author Radoslav Dutina
* @version 1.0
*/
public class DBConnectionManager {
private Vector drivers=new Vector();
private Hashtable pools=new Hashtable();
private static DBConnectionManager instance; // The single instance
private Hashtable openConnections=new Hashtable();
private Vector allConnectons=new Vector();
public String loaderJobPath="";
public String connectinPrefix="";
public boolean fileSystemDatabase=false;
/**
* This method set all connection into vector
* @param allConnections represents all connection property
*/
public DBConnectionManager(Vector allConnections) {
this.allConnectons=allConnections;
init();
}
/**
* This method set path to LoaderJob.olj file
* @param loaderJob represents string path to LoaderJob.olj file
*/
public void setLoaderJobPath(String loaderJob){
File file=new File(loaderJob);
this.loaderJobPath=file.getAbsoluteFile().getParent()+System.getProperty("file.separator");
}
/**
* This method set connection prefix
* @param prefix is value of connection prefix
*/
public void setConnectionPrefix(String prefix){
this.connectinPrefix=prefix;
}
/**
* This method set the value of fileSystemDatabase parameter
* @param doParse is value of parameter
*/
public void setParsePermission(boolean doParse){
this.fileSystemDatabase=doParse;
}
/**
* This method initialized DBConnectionManager object
*/
private void init(){
loadDrivers(allConnectons);
createPools(allConnectons);
}
private void loadDrivers(Vector allConnectons){
if(allConnectons.size()!=0){
for(int i=0;i<allConnectons.size();i=i+4){
if(i>3){
if(allConnectons.get(i).toString().equalsIgnoreCase(allConnectons.get(i-4).toString())){
//do nothing
}else{
try{
Driver driver=(Driver)Class.forName(allConnectons.get(i).toString()).newInstance();
DriverManager.registerDriver(driver);
drivers.add(driver);
}catch(Exception e){
e.getMessage();
}
}
}else{
try{
Driver driver=(Driver)Class.forName(allConnectons.get(i).toString()).newInstance();
DriverManager.registerDriver(driver);
drivers.add(driver);
}catch(Exception e){
e.getMessage();
}
}
}
}
}
private void createPools(Vector allConnectons){
for(int i=0;i<allConnectons.size();i=i+4){
String url=allConnectons.get(i+1).toString();
if(url.indexOf("jdbc:microsoft:sqlserver")!=-1) {
if(url.indexOf("SelectMethod")==-1) {
url = url+";SelectMethod=cursor";
}
}
String poolName=url;
String user=allConnectons.get(i+2).toString();
String password=allConnectons.get(i+3).toString();
boolean check=false;
if(pools.size()>0){
check=pools.containsKey(poolName);
}
if(!check){
DBConnectionPool pool=new DBConnectionPool(poolName,url,user,password);
pools.put(poolName,pool);
}
}
}
/**
* This method get connection from connection pool
* @param name is the name of the pool
* @return connection
*/
public Connection getConnection(String name){
DBConnectionPool pool=(DBConnectionPool)pools.get(name);
if(pool!=null){
return pool.getConnection();
}
return null;
}
/**
* This method release (close) all connection and deregister all drivers
* @param exception defines if application calls release from exception method or not
*/
public void release(String exception){
Enumeration allPools=pools.elements();
while(allPools.hasMoreElements()){
DBConnectionPool pool=(DBConnectionPool)allPools.nextElement();
pool.release(exception);
}
Enumeration allDrivers=drivers.elements();
while(allDrivers.hasMoreElements()){
Driver driver=(Driver)allDrivers.nextElement();
try{
DriverManager.deregisterDriver(driver);
}catch(Exception e){
e.getMessage();
}
}
}
private class DBConnectionPool{
private String name=null;
private String url=null;
private String user=null;
private String password=null;
/**
* Construct the object of DBConnectionPool class with associated parameters
* @param name is the name of the connection
* @param url is url to database which we wont to connect
* @param user is the user name
* @param password is user password
*/
public DBConnectionPool(String name,String url,String user,String password){
this.name=name;
this.url=url;
this.user=user;
this.password=password;
}
/**
* This method return connection if the connection exists, or create new connection
* if don't
* @return connection
*/
public Connection getConnection(){
Connection conn=null;
if(openConnections.size()>0){
conn=(Connection)openConnections.get(name);
if(conn!=null){
return conn;
}
conn=newConnections();
}else{
conn=newConnections();
}
return conn;
}
private Connection newConnections(){
Connection conn=null;
url=Utils.getAbsolutePathFromDatabaseURL(connectinPrefix,loaderJobPath,
url,fileSystemDatabase);
try{
if(user==null){
conn=DriverManager.getConnection(url);
openConnections.put(name,conn);
}else{
conn=DriverManager.getConnection(url,user,password);
openConnections.put(name,conn);
}
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
/**
* This method close and commit connection
* @param exception defines if application calls release from exception method or not
*/
public void release(String exception){
Connection conn=(Connection)openConnections.get(name);
try{
if(!conn.isClosed()){
if(exception.equalsIgnoreCase("false"))
conn.commit();
conn.close();
}
openConnections.remove(name);
}catch(Exception e){
e.getMessage();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -