配置多数据源

1.基于注解的Spring多数据源配置和使用,这个博主说的很清晰https://www.cnblogs.com/liujiduo/p/5004691.html

2.关于jeesite配置多数据源https://www.cnblogs.com/xiaomatech/p/5280594.html
描述的一样,但是调用有所不同。jeesite切换数据源最好在controller里,因为在dao层和service里切换可能无效。

/*	    ShopMember shopMember = new ShopMember();
		DynamicDataSource.setCurrentLookupKey("dataSource2");
		ShopMember memberList = shopMemberService.selectMemberPpoints(owner.getOwnerPhone());
		if(memberList!=null){
		shopPropointsLogService.insert(shopPropointsLog);
		shopMemberService.update(shopMember);
		}*/
		//DynamicDataSource.setCurrentLookupKey("dataSource");再次切换回来

3.C3P0切换数据源

3.1) 
@Component("c3P0Util")
public class C3P0Util {
private ComboPooledDataSource dataSource1 = null;
private ComboPooledDataSource dataSource12 = null;

private C3P0Util() throws PropertyVetoException {
	
	dataSource1 = new ComboPooledDataSource("dataSource1");
	String driverClassName = "com.mysql.jdbc.Driver";
	String url = "url";
	String username = "root"; 
	String password = "root";
	
	/** 设置数据库连接驱动 **/
	dataSource1.setDriverClass(driverClassName);
	/** 设置数据库连接地址 **/
	dataSource1.setJdbcUrl(url);
	/** 设置数据库连接用户名 **/
	dataSource1.setUser(username);
	/** 设置数据库连接密码 **/
	dataSource1.setPassword(password);
	dataSource1.setMaxPoolSize(200);
	dataSource1.setMinPoolSize(50);
	/** 初始化时创建的连接数,应在minPoolSize与maxPoolSize之间取值.默认为3 **/
	dataSource1.setInitialPoolSize(20);
	/** 连接池中保留的最大连接数据.默认为15 **/
	// cpds.setMaxPoolSize(10);
	/** 当连接池中的连接用完时,C3PO一次性创建新的连接数目; **/
	dataSource1.setAcquireIncrement(2);
	dataSource1.setCheckoutTimeout(3000);
	/** 隔多少秒检查所有连接池中的空闲连接,默认为0表示不检查; **/
	dataSource1.setIdleConnectionTestPeriod(60);
	/** 最大空闲时间,超过空闲时间的连接将被丢弃.为0或负数据则永不丢弃.默认为0; **/
	dataSource1.setMaxIdleTime(60);

	/**
	 * 因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的
	 * 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable
	 * 等方法来提升连接测试的性能。Default: false
	 **/
	dataSource1.setTestConnectionOnCheckout(false);

	/** 如果设为true那么在取得连接的同时将校验连接的有效性。Default: false **/
	dataSource1.setTestConnectionOnCheckin(false);
	/** 定义在从数据库获取新的连接失败后重复尝试获取的次数,默认为30; **/
	dataSource1.setAcquireRetryAttempts(0);
	/** 两次连接中间隔时间默认为1000毫秒 **/
	dataSource1.setAcquireRetryDelay(1000);
	dataSource1.setAutoCommitOnClose(false);
	dataSource1.setAutomaticTestTable("Test");
	/**
	 * 获取连接失败将会引起所有等待获取连接的线程异常,
	 * 但是数据源仍有效的保留,并在下次调用getConnection()的时候继续尝试获取连接.如果设为true,
	 * 那么尝试获取连接失败后该数据源将申明已经断开并永久关闭.默认为false
	 **/
	dataSource1.setBreakAfterAcquireFailure(false);
	
	// 商城数据源
	dataSource2 = new ComboPooledDataSource("dataSource2");
	String driverClassName2 = "com.mysql.jdbc.Driver";
	

	//	测试
	String url2 = "url2";
	String username2 = "root"; 
	String password2 = "123";
	/** 设置数据库连接驱动 **/
	dataSource2.setDriverClass(driverClassName2);
	/** 设置数据库连接地址 **/
	dataSource2.setJdbcUrl(url2);
	/** 设置数据库连接用户名 **/
	dataSource2.setUser(username2);
	/** 设置数据库连接密码 **/
	dataSource2.setPassword(password2);
	dataSource2.setMaxPoolSize(100);
	dataSource2.setMinPoolSize(20);
	/** 初始化时创建的连接数,应在minPoolSize与maxPoolSize之间取值.默认为3 **/
	dataSource2.setInitialPoolSize(20);
	/** 连接池中保留的最大连接数据.默认为15 **/
	// cpds.setMaxPoolSize(10);
	/** 当连接池中的连接用完时,C3PO一次性创建新的连接数目; **/
	dataSource2.setAcquireIncrement(2);
	dataSource2.setCheckoutTimeout(3000);
	/** 隔多少秒检查所有连接池中的空闲连接,默认为0表示不检查; **/
	dataSource2.setIdleConnectionTestPeriod(60);
	/** 最大空闲时间,超过空闲时间的连接将被丢弃.为0或负数据则永不丢弃.默认为0; **/
	dataSource2.setMaxIdleTime(60);

	/**
	 * 因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的
	 * 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable
	 * 等方法来提升连接测试的性能。Default: false
	 **/
	dataSource2.setTestConnectionOnCheckout(false);

	/** 如果设为true那么在取得连接的同时将校验连接的有效性。Default: false **/
	dataSource2.setTestConnectionOnCheckin(false);
	/** 定义在从数据库获取新的连接失败后重复尝试获取的次数,默认为30; **/
	dataSource2.setAcquireRetryAttempts(0);
	/** 两次连接中间隔时间默认为1000毫秒 **/
	dataSource2.setAcquireRetryDelay(1000);
	dataSource2.setAutoCommitOnClose(false);
	dataSource2.setAutomaticTestTable("Test");
	/**
	 * 获取连接失败将会引起所有等待获取连接的线程异常,
	 * 但是数据源仍有效的保留,并在下次调用getConnection()的时候继续尝试获取连接.如果设为true,
	 * 那么尝试获取连接失败后该数据源将申明已经断开并永久关闭.默认为false
	 **/
	shop.setBreakAfterAcquireFailure(false);
}


public synchronized Connection getConnection(String dataSource)
		throws SQLException {
	if ("dataSource1".equals(dataSource)) {
		return dataSource1.getConnection();
	} else if ("dataSource2".equals(dataSource)) {
		return dataSource2.getConnection();
	}
	return null;
}

public synchronized void close(Connection conn) {
	try {
		if (conn != null) {
			conn.close();
			conn = null;
		}
	} catch (SQLException e) {
	}
}

public synchronized void close(Statement stat) {
	try {
		if (stat != null) {
			stat.close();
			stat = null;
		}
	} catch (SQLException e) {
	}
}

public synchronized void close(ResultSet rest) {
	try {
		if (rest != null) {
			rest.close();
			rest = null;
		}
	} catch (SQLException e) {
	}
}}
3.2)
	
@Component("JdbcMapper")
public class JdbcMapper {

private Logger logger = LoggerFactory.getLogger(JdbcMapper.class);

@Autowired
C3P0Util c3P0Util;

public C3P0Util getC3P0Util() {
	return c3P0Util;
}

public void setC3P0Util(C3P0Util c3p0Util) {
	c3P0Util = c3p0Util;
}
/**
 * 
 * @param userPhone
 * @return
 */
public int getMemberIdByUserPhone(String userPhone) {
	int i = 0;
	Connection watcon = null;
	try {
		watcon = c3P0Util.getConnection("dataSource2");
		Statement state = watcon.createStatement();
		String sql = "sql " + userPhone;
		ResultSet rs = state.executeQuery(sql);
		while (rs.next()) {
			i = rs.getInt("参数");
		}
	} catch (Exception e) {
		logger.error(e.getMessage(), e);
		c3P0Util.close(watcon);
		e.printStackTrace();
	}
	c3P0Util.close(watcon);
	return i;
}	
}	

猜你喜欢

转载自blog.csdn.net/sijielai6273/article/details/85680310