All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tsc9526.monalisa.orm.datasource.SimpleDataSource Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
/*******************************************************************************************
 *	Copyright (c) 2016, zzg.zhou([email protected])
 * 
 *  Monalisa 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 3 of the License, or
 *	(at your option) any later version.

 *	This program 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 program.  If not, see .
 *******************************************************************************************/
package com.tsc9526.monalisa.orm.datasource;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Semaphore;

import com.tsc9526.monalisa.tools.clazz.MelpClass;
import com.tsc9526.monalisa.tools.clazz.MelpLib;
import com.tsc9526.monalisa.tools.logger.Logger;

/**
 *  
 * @author zzg.zhou([email protected])
 */
public class SimpleDataSource implements PooledDataSource {	 
	static Logger logger=Logger.getLogger(SimpleDataSource.class);
	
	private ConcurrentMap pool = new ConcurrentHashMap();
	
	private String url;
	private String driver;
	private String username;
	private String password;
 	
	private int maxSize;
	private int minSize;
	private Semaphore semaphore;

	private Properties connProps=new Properties();
	
	public SimpleDataSource(DBConfig db) {
		this(db, null);
	}
	
	public SimpleDataSource(DBConfig db,Properties poolProperties) {
		String driver=db.getDialect().getDriver();
		String v=db.getCfg().getDriver();
		if(v!=null && v.length()>1){
			driver=v;
		}
		
		setDriver(driver);
		setUrl(db.getCfg().getUrl());
		setUsername(db.getCfg().getUsername());
		setPassword(db.getCfg().getPassword());
		 
		connProps.put("user",getUsername());     
		connProps.put("password",getPassword());     
		connProps.putAll(db.getCfg().getPoolProperties() );
		if(poolProperties!=null){
			connProps.putAll(poolProperties);
		}

		maxSize = db.getCfg().getProperty("pool.max", 50);
		minSize = db.getCfg().getProperty("pool.min", 3);
		
		MelpLib.loadClass(driver);
		
		initConnections(db);
	}
	
	private void initConnections(DBConfig db){
		logger.info("Initializing simple data source{ pool.max = "+maxSize+", pool.min = "+minSize+", jdbcUrl = "+db.getCfg().getUrl()+", username = "+ db.getCfg().getUsername() +"}");
		
		semaphore = new Semaphore(maxSize, false);
		
		if(minSize>0 && minSize < maxSize){
			try{
				List connections=new ArrayList();
				for(int i=0;i T unwrap(Class iface) throws SQLException {
		return null;
	}

	public boolean isWrapperFor(Class iface) throws SQLException {
		return false;
	}

	public void setUrl(String url) {
		this.url = url;

	}

	public void setDriver(String driver) {
		this.driver = driver;

	}

	public void setUsername(String username) {
		this.username = username;

	}

	public void setPassword(String password) {
		this.password = password;

	}

	public String getUrl() {
		return url;
	}

	public String getDriver() {
		return driver;
	}

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}

	public void setIdleValidationQuery(int idleInSeconds,String validationQuery){
		//do noting
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy