package com.huawei.db;
import java.sql.*;
import java.io.*;
import javax.sql.DataSource;
public class ConnectionPoolFactory
{
protected static final String DEFAULT_POOL_NAME = "ORACLE";
protected static final String DEFAULT_DB_TYPE = "DbType";
protected static boolean dbFlag = true;
private static ConnectionPool pool = null;
static
{
try
{
init();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void init() throws IOException, SQLException
{
if ("JDBC".equals(Configuration.getProperty(DEFAULT_DB_TYPE)))
{
dbFlag = false;
synchronized(ConnectionPoolFactory.class)
{
if(pool == null)
{
pool = getConnectionPool();
}
}
}
else
{
dbFlag = true;
pool = null;
}
}
public static ConnectionPool getConnectionPool(
String poolName,
String driver, String url,
String username, String password,
int initialConnections,
int maxConnections,
int lockwait
) throws IOException, SQLException
{
if (poolName == null || poolName.equals(""))
poolName = DEFAULT_POOL_NAME;
if ("ORACLE".equals(poolName)) {
return new DefaultConnectionPool(driver, url, username, password,
initialConnections, maxConnections, true);
}
else
throw new RuntimeException("Connection Name is not existed [" + poolName +
"]");
}
public static ConnectionPool getConnectionPool(String configname) throws IOException, SQLException {
String poolName;
String driver;
String url;
String username;
String password;
int initnumber;
int maxnumber;
int lockwait = 10;
poolName = Configuration.getProperty(configname + ".name", "ORACLE");
driver = Configuration.getProperty(configname + ".driver","oracle.jdbc.driver.OracleDriver");
url = Configuration.getProperty(configname + ".url","jdbc:oracle:thin:@10.71.181.226:1521:WPSDB");
username = Configuration.getProperty(configname + ".username", "esb");
password = Configuration.getProperty(configname + ".password", "esb");
initnumber = Integer.parseInt(Configuration.getProperty(configname +
".initnumber", "10"));
maxnumber = Integer.parseInt(Configuration.getProperty(configname +
".maxnumber", "10"));
try {
lockwait = Integer.parseInt(Configuration.getProperty(configname +
".lockwait", "10"));
} catch (NumberFormatException ex) {}
return getConnectionPool(poolName, driver, url, username, password,
initnumber, maxnumber,lockwait);
}
/**
* 通过JDBC的方式后去连接
* @return
* @throws IOException
* @throws SQLException
*/
public static ConnectionPool getConnectionPool() throws IOException,SQLException {
return getConnectionPool("defaultdb");
}
/**
* 通过数据源的方式获取连接
* @param jndiName
* @return
* @throws IOException
* @throws SQLException
*/
public static DataSource getDataSouce() throws IOException, SQLException
{
return getDataSouce("DataSource");
}
public static DataSource getDataSouce(String dsName) throws IOException, SQLException
{
return new DataSourceConnectionPool(dsName).getDataSource();
}
public static Connection getConnection() throws SQLException, IOException
{
if (dbFlag)
{
return getDataSouce().getConnection();
}
else
{
if (pool == null)
init();
return pool.getConnection();
}
}
public static void freeConn(Connection conn)
{
if (pool != null)
{
pool.free(conn);
}
else
{
DBTools.close(conn);
}
}
}