⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectionprovider.java

📁 JSP聊天系统
💻 JAVA
字号:
package org.ehotsoft.yekki.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Properties;

import org.ehotsoft.yekki.sql.pool.ConnectionPoolManager;
import org.ehotsoft.yekki.util.PropertyManager;

public class ConnectionProvider {


    private ConnectionPoolManager cpm = null;
    private Properties props;
    private Properties propDescriptions;

    private Object initLock = new Object();

    public ConnectionProvider() {
        props = new Properties();
        propDescriptions = new Properties();
        initializeProperties();
        loadProperties();
		start();
    }
	
	public Connection getConnection() {

        if (  cpm == null ) {
        
			synchronized(  initLock  ) {
                if ( cpm == null ) {
                    System.err.println( "Warning: ConnectionProvider.getConnection() was " +
                    "called when the internal pool has not been initialized." );
                    return null;
                }
            }
        }
		
		Connection cnn = null;

		try {
			
			cnn = DriverManager.getConnection( ConnectionPoolManager.URL_PREFIX + "yekki", null, null );
		}
		catch ( SQLException e ) {
			
			System.err.println( "Warning:DriverManager.getConnection() was called but errors occurs:" + e.getMessage() );
		}

        return cnn;
    }
	
	 protected void start() {

		synchronized ( initLock ) {

			String driver = props.getProperty( "driver" );
			String server = props.getProperty( "server" );
			String username = props.getProperty( "username" );
			String password = props.getProperty( "password" );
			int maxConnections = 0;
			int idleTimeout = 0;
			int checkoutTimeout = 0;
			int maxCheckout = 0;
			boolean cacheStatements = true;
			boolean track = true;
			try {
				maxConnections = Integer.parseInt( props.getProperty( "maxConnections" ) );
				idleTimeout = Integer.parseInt( props.getProperty( "idleTimeout" ) );
				checkoutTimeout = Integer.parseInt( props.getProperty( "checkoutTimeout" ) );
				maxCheckout = Integer.parseInt( props.getProperty( "maxCheckout" ) );
				cacheStatements = Boolean.valueOf( props.getProperty( "cacheStatements" ) ).booleanValue();
				track = Boolean.valueOf( props.getProperty( "track" ) ).booleanValue();
			}
			catch ( Exception e ) {
				System.err.println( "Error: could not parse default pool properties. " + "Make sure the values exist and are correct." );
				e.printStackTrace();
				return;
			}
			
			try {

				cpm = new ConnectionPoolManager( 300 );
				Class.forName( driver ).newInstance();
				cpm.addAlias( "yekki", driver,
						server,
						username,
						password,
						maxConnections,
						idleTimeout,
						checkoutTimeout,
						maxCheckout,
						cacheStatements );
				
				cpm.setTracing( track );//for debug
			}
			catch ( Exception ioe ) {
				System.err.println( "Error starting DbConnectionDefaultPool: " + ioe );
				ioe.printStackTrace();
			}
		}
	}

    public String getProperty( String name ) {
        return ( String )props.get( name );
    }

    
    public String getPropertyDescription( String name ) {
        return ( String )propDescriptions.get( name );
    }

  
    public Enumeration propertyNames() {
        return props.propertyNames();
    }

    
    public void setProperty( String name, String value ) {
        props.put( name, value );
        saveProperties();
    }

    
    private void initializeProperties() {
        props.put( "driver","" );
        props.put( "server","" );
        props.put( "username","" );
        props.put( "password","" );
        props.put( "maxConnections","" );
        props.put( "idleTimeout","" );
        props.put( "checkoutTimeout","" );
        props.put( "maxCheckout","" );
		props.put( "cacheStatements","" );
		props.put( "track", "" );

        propDescriptions.put( "driver","JDBC driver. e.g. 'oracle.jdbc.driver.OracleDriver'" );
        propDescriptions.put( "server","JDBC connect string. e.g. 'jdbc:oracle:thin:@203.92.21.109:1526:orcl'" );
        propDescriptions.put( "username","Database username. e.g. 'Scott'" );
        propDescriptions.put( "password","Database password. e.g. 'Tiger'" );
        propDescriptions.put( "maxConnections","max connections to open" );
        propDescriptions.put( "idleTimeout","seconds a connection can be idle before it is closed" );
        propDescriptions.put( "checkoutTimeout","seconds a connection can be checked out by a thread before it is returned back to the pool" );
        propDescriptions.put( "maxCheckout","number of times a connection can be re-used before connection to database is closed and re-opened" );
		propDescriptions.put( "cacheStatements","specifies whether to cache statements" );
		propDescriptions.put( "track","track connection pool for debugging" );
    }

   
    private void loadProperties() {

        String driver = PropertyManager.getProperty( "ConnectionPoolManager.driver" );
        String server = PropertyManager.getProperty( "ConnectionPoolManager.server" );
        String username = PropertyManager.getProperty( "ConnectionPoolManager.username" );
        String password = PropertyManager.getProperty( "ConnectionPoolManager.password" );
        String maxConnections = PropertyManager.getProperty( "ConnectionPoolManager.maxConnections" );
        String idleTimeout = PropertyManager.getProperty( "ConnectionPoolManager.idleTimeout" );
        String checkoutTimeout = PropertyManager.getProperty( "ConnectionPoolManager.checkoutTimeout" );
        String maxCheckout = PropertyManager.getProperty( "ConnectionPoolManager.maxCheckout" );
		String cacheStatements = PropertyManager.getProperty( "ConnectionPoolManager.cacheStatements" );
		String track = PropertyManager.getProperty( "ConnectionPoolManager.track" );
        if ( driver != null ) {  props.setProperty( "driver", driver );  }
        if ( server != null ) {  props.setProperty( "server", server );  }
        if ( username != null ) {  props.setProperty( "username", username );  }
        if ( password != null ) {  props.setProperty( "password", password );  }
        if ( maxConnections != null ) {  props.setProperty( "maxConnections", maxConnections );  }
        if ( idleTimeout != null ) {  props.setProperty( "idleTimeout", idleTimeout );  }
        if ( checkoutTimeout != null ) {  props.setProperty( "checkoutTimeout", checkoutTimeout );  }
        if ( maxCheckout != null ) {  props.setProperty( "maxCheckout", maxCheckout );  }
		if ( cacheStatements != null ) {  props.setProperty( "cacheStatements", cacheStatements );  }
		if ( track != null ) { props.setProperty( "track", track ); }
    }

    private void saveProperties() {

        PropertyManager.setProperty( "ConnectionPoolManager.driver", props.getProperty( "driver" ) );
        PropertyManager.setProperty( "ConnectionPoolManager.server", props.getProperty( "server" ) );
        PropertyManager.setProperty( "ConnectionPoolManager.username", props.getProperty( "username" ) );
        PropertyManager.setProperty( "ConnectionPoolManager.password", props.getProperty( "password" ) );
        PropertyManager.setProperty( "ConnectionPoolManager.maxConnections", props.getProperty( "maxConnections" ) );
        PropertyManager.setProperty( "ConnectionPoolManager.idleTimeout", props.getProperty( "idleTimeout" ) );
        PropertyManager.setProperty( "ConnectionPoolManager.checkoutTimeout", props.getProperty( "checkoutTimeout" ) );
        PropertyManager.setProperty( "ConnectionPoolManager.maxCheckout", props.getProperty( "maxCheckout" ) );
		PropertyManager.setProperty( "ConnectionPoolManager.cacheStatements", props.getProperty( "cacheStatements" ) );
		PropertyManager.setProperty( "ConnectionPoolManager.track", props.getProperty( "track" ) );
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -