Browse Source

支持数据源切换;引入管理平台数据源

git-svn-id: svn+ssh://10.10.101.21/source/platform/platform-b2b@3192 f3bf4e98-0cf0-11e4-a00c-a99a8b9d557d
yingp 10 years ago
parent
commit
e355f51604

+ 90 - 0
src/main/java/com/uas/platform/b2b/data/support/MultiDataSource.java

@@ -0,0 +1,90 @@
+package com.uas.platform.b2b.data.support;
+
+import java.io.PrintWriter;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.util.Properties;
+import java.util.logging.Logger;
+
+import javax.sql.DataSource;
+
+import org.apache.commons.dbcp.BasicDataSource;
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+
+public class MultiDataSource extends BasicDataSource implements DataSource, ApplicationContextAware {
+
+	private ApplicationContext applicationContext = null;
+	private DataSource dataSource = null;
+	private Properties connectionProperties = null;
+
+	public Connection getConnection() throws SQLException {
+		return getDataSource().getConnection();
+	}
+
+	public Connection getConnection(String arg0, String arg1) throws SQLException {
+		return getDataSource().getConnection(arg0, arg1);
+	}
+
+	public PrintWriter getLogWriter() throws SQLException {
+		return getDataSource().getLogWriter();
+	}
+
+	public Properties getConnectionProperties() {
+		return connectionProperties;
+	}
+
+	public void setConnectionProperties(Properties connectionProperties) {
+		this.connectionProperties = connectionProperties;
+	}
+
+	public int getLoginTimeout() throws SQLException {
+		return getDataSource().getLoginTimeout();
+	}
+
+	public void setLogWriter(PrintWriter arg0) throws SQLException {
+		getDataSource().setLogWriter(arg0);
+	}
+
+	public void setLoginTimeout(int arg0) throws SQLException {
+		getDataSource().setLoginTimeout(arg0);
+	}
+
+	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
+		this.applicationContext = applicationContext;
+	}
+
+	public DataSource getDataSource(String dataSourceName) {
+		if (dataSourceName == null || dataSourceName.equals("")) {
+			return this.dataSource;
+		}
+		return (DataSource) this.applicationContext.getBean(dataSourceName);
+	}
+
+	public void setDataSource(DataSource dataSource) {
+		this.dataSource = dataSource;
+	}
+
+	public DataSource getDataSource() {
+		String sp = SpObserver.getSp();
+		return getDataSource(sp);
+	}
+
+	@Override
+	public <T> T unwrap(Class<T> iface) throws SQLException {
+		return null;
+	}
+
+	@Override
+	public boolean isWrapperFor(Class<?> iface) throws SQLException {
+		return false;
+	}
+
+	@Override
+	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
+		return null;
+	}
+
+}

+ 23 - 0
src/main/java/com/uas/platform/b2b/data/support/SpObserver.java

@@ -0,0 +1,23 @@
+package com.uas.platform.b2b.data.support;
+
+/**
+ * 
+ */
+public class SpObserver {
+	
+	private static ThreadLocal<String> local = new ThreadLocal<String>();
+
+	/**
+	 * 切换数据源
+	 * 
+	 * @param sp
+	 *            dbsource name
+	 */
+	public static void putSp(String sp) {
+		local.set(sp);
+	}
+
+	public static String getSp() {
+		return (String) local.get();
+	}
+}

+ 7 - 1
src/main/resources/jdbc.properties

@@ -5,4 +5,10 @@ jdbc.maxIdle=50
 jdbc.minIdle=50
 jdbc.suspectTimeout=60
 jdbc.timeBetweenEvictionRunsMillis=30000
-jdbc.minEvictableIdleTimeMillis=60000
+jdbc.minEvictableIdleTimeMillis=60000
+
+#manage.ubtob.com
+manage.jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
+manage.jdbc.url=jdbc:oracle:thin:@//10.10.100.215:1521/orcl
+manage.jdbc.username=platformmanager
+manage.jdbc.password=select!#%*(

+ 42 - 1
src/main/resources/spring/context.xml

@@ -43,7 +43,20 @@
 		<property name="cacheSeconds" value="60" />
 	</bean>
 
-	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
+	<bean id="dataSource" class="com.uas.platform.b2b.data.support.MultiDataSource">
+		<property name="dataSource">
+			<ref bean="defaultDataSource" />
+		</property>
+		<property name="connectionProperties">
+			<props>
+				<prop key="oracle.net.CONNECT_TIMEOUT">6000</prop>
+				<prop key="oracle.jdbc.ReadTimeout">6000</prop>
+				<prop key="oracle.net.READ_TIMEOUT">6000</prop>
+			</props>
+		</property>
+	</bean>
+
+	<bean id="defaultDataSource" class="org.apache.commons.dbcp.BasicDataSource"
 		destroy-method="close">
 		<property name="driverClassName" value="${jdbc.driverClassName}" />
 		<property name="url" value="${profile-jdbc-url}" />
@@ -71,6 +84,34 @@
 		<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" />
 		<property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" />
 	</bean>
+	<bean id="manageDataSource" class="org.apache.commons.dbcp.BasicDataSource"
+		destroy-method="close">
+		<property name="driverClassName" value="${manage.jdbc.driverClassName}" />
+		<property name="url" value="${manage.jdbc.url}" />
+		<property name="username" value="${manage.jdbc.username}" />
+		<property name="password" value="${manage.jdbc.password}" />
+		<!-- 连接初始值,连接池启动时创建的连接数量的初始值 -->
+		<property name="initialSize" value="${jdbc.initialSize}" />
+		<!-- 连接池的最大值,同一时间可以从池分配的最多连接数量,0时无限制 -->
+		<property name="maxActive" value="${jdbc.maxActive}" />
+		<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 ,0时无限制 -->
+		<property name="maxIdle" value="${jdbc.maxIdle}" />
+		<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
+		<property name="minIdle" value="${jdbc.minIdle}" />
+		<!-- 是否对已备语句进行池管理(布尔值),是否对PreparedStatement进行缓存 -->
+		<property name="poolPreparedStatements" value="true" />
+		<!-- 是否对sql进行自动提交 -->
+		<property name="defaultAutoCommit" value="true" />
+		<!-- 回收超时连接 -->
+		<property name="removeAbandoned" value="true" />
+		<!-- 连接空闲时校验连接有效性 -->
+		<property name="testWhileIdle" value="true" />
+		<!-- 校验连接有效性的sql -->
+		<property name="validationQuery" value="select 1 from dual" />
+		<!-- 每过timeBetweenEvictionRunsMillis 时间,就会启动一个线程,校验连接池中闲置时间超过minEvictableIdleTimeMillis的连接对象 -->
+		<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" />
+		<property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" />
+	</bean>
 	<!-- hibernate jpa -->
 	<bean class="org.springframework.orm.jpa.JpaTransactionManager"
 		id="transactionManager">