package com.smart.ibus.ap.das.datasource;
/**
* DataSourceFactory.java
*
* (C)CopyRight 2007-2008 粤ICP备 05009745 All right reserved.
* 版权所有,广东智慧电子信息产业股份有限公司
* 地址:广州市中山大道西天河软件园建工路八号海旺工业大厦
*
* Created on 2011-4-19
*/
import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import javax.sql.DataSource;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
* <p>
* 类功能描述,要求清楚、明了,含义准确(*)
* </p>
* <p>
* <b>修改历史</b>
* </p>
* <ol>
* <li>举例:新增foo(String arg1)方法(Added by Henry on 2009-2-14)</li>
* </ol>
* @author Administrator
* @version 1.0
*/
public class DataSourceFactory {
private static HashMap<String, DataSource> dsMap = new HashMap<String, DataSource>();
private static ApplicationContext context;
public synchronized static Object getSpringService(String beanId) {
if (context == null) {
context = new ClassPathXmlApplicationContext(new String[]{"applicationContext-jdbc.xml"});
//context = new FileSystemXmlApplicationContext("src/applicationContext.xml");
}
return context.getBean(beanId);
}
static {
try {
String path = DataSourceFactory.class.getResource("/applicationContext-jdbc.xml").getPath();
SAXReader reader = new SAXReader();
Document doc = reader.read(new File(path));
List<?> beans = doc.getRootElement().elements();
for (Object o : beans) {
Element bean = (Element) o;
String id = bean.attributeValue("id");
String className = bean.attributeValue("class");
if (DataSourceFactory.class.getClassLoader()
.loadClass(className).newInstance() instanceof DataSource) {
dsMap.put(id, (DataSource) DataSourceFactory.getSpringService(id));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static DataSource getDataSource(String dsName) {
return dsMap.get(dsName);
}
public static Connection getConnectionWithName(String dsName)
throws SQLException {
return dsMap.get(dsName).getConnection();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="default" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 最大活动连接数 -->
<property name="maxActive">
<value>100</value>
</property>
<!-- 最长等待时间 -->
<property name="maxWait">
<value>1000</value>
</property>
<property name="maxIdle">
<value>30</value>
</property>
<property name="defaultAutoCommit">
<value>true</value>
</property>
<!-- 自动回收连接池,避免连接池泄露 -->
<property name="removeAbandoned">
<value>true</value>
</property>
<property name="removeAbandonedTimeout">
<value>60</value>
</property>
</bean>
<bean id="smart_ibusV3K" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.old.url}" />
<property name="username" value="${jdbc.old.username}" />
<property name="password" value="${jdbc.old.password}" />
<!-- 最大活动连接数 -->
<property name="maxActive">
<value>100</value>
</property>
<!-- 最长等待时间 -->
<property name="maxWait">
<value>1000</value>
</property>
<property name="maxIdle">
<value>30</value>
</property>
<property name="defaultAutoCommit">
<value>true</value>
</property>
<!-- 自动回收连接池,避免连接池泄露 -->
<property name="removeAbandoned">
<value>true</value>
</property>
<property name="removeAbandonedTimeout">
<value>60</value>
</property>
</bean>
<bean id="ibus4boss" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.ibus4boss.url}" />4
<property name="username" value="${jdbc.ibus4boss.username}" />
<property name="password" value="${jdbc.ibus4boss.password}" />
<!-- 最大活动连接数 -->
<property name="maxActive">
<value>100</value>
</property>
<!-- 最长等待时间 -->
<property name="maxWait">
<value>1000</value>
</property>
<property name="maxIdle">
<value>30</value>
</property>
<property name="defaultAutoCommit">
<value>true</value>
</property>
<!-- 自动回收连接池,避免连接池泄露 -->
<property name="removeAbandoned">
<value>true</value>
</property>
<property name="removeAbandonedTimeout">
<value>60</value>
</property>
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
</beans>
jdbc.properties
## for sql server.
#jdbc.driver=net.sourceforge.jtds.jdbc.Driver
#jdbc.url=jdbc:jtds:sqlserver://192.169.0.171:1433;DatabaseName=platform
#jdbc.username=sa
#jdbc.password=123
#jdbc.dialect=org.hibernate.dialect.SQLServerDialect
# for oracle.
jdbc.driver=oracle.jdbc.OracleDriver
# for ibusap
jdbc.url=jdbc:oracle:thin:@192.168.7.55:1521:IBUSDB
jdbc.username=ibusap
jdbc.password=ibusap
# for old-ibus(smart-ibus3k project)
jdbc.old.url=jdbc:oracle:thin:@192.168.7.55:1521:IBUSDB
jdbc.old.username=smart_ibusV3K
jdbc.old.password=smart
# for boss(smart-ibus3k project)
jdbc.ibus4boss.url=jdbc:oracle:thin:@192.168.7.55:1521:IBUSDB
jdbc.ibus4boss.username=ibus4boss
jdbc.ibus4boss.password=ibus