<commit_before>/*
 *************************************************************************
 * Copyright (c) 2004, 2005 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *  
 *************************************************************************
 */ 
package org.eclipse.birt.data.engine.executor;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.birt.data.engine.api.IShutdownListener;
import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.i18n.ResourceConstants;
import org.eclipse.birt.data.engine.impl.DataEngineSession;
import org.eclipse.birt.data.engine.odaconsumer.Connection;
import org.eclipse.birt.data.engine.odaconsumer.ConnectionManager;
import org.eclipse.birt.data.engine.odaconsumer.PreparedStatement;
import org.eclipse.birt.data.engine.odi.ICandidateQuery;
import org.eclipse.birt.data.engine.odi.IDataSource;
import org.eclipse.birt.data.engine.odi.IDataSourceQuery;

/**
 * Implementation of ODI's IDataSource interface
 */
class DataSource implements IDataSource
{
	private String 		driverName;
    private Map			appContext;
    private Properties	connectionProps = new Properties();
    
    // A pool of open odaconsumer.Connection. Since each connection may support a limited
	// # of statements, we may need to use more than one connection to handle concurrent statements
	// This is a set of OpenConnection

	
	private static Map<DataEngineSession, Map<ConnectionProp, Set<CacheConnection>>> dataEngineLevelConnectionPool = new Hashtable<DataEngineSession, Map<ConnectionProp, Set<CacheConnection>>>( );
	
	// Currently active oda Statements. This is a map from PreparedStatement to OpenConnection
	private HashMap statementMap = new HashMap();
	
	private static String className = DataSource.class.getName();
	private static Logger logger = Logger.getLogger( className ); 

	private DataEngineSession session;
	
	/**
	 * 
	 * @param driverName
	 * @param connProperties
	 * @param session
	 * @param info
	 */
    public DataSource( String driverName, Map connProperties,
			DataEngineSession session )
	{
    	this.driverName = driverName;
    	if ( connProperties != null )
    		this.connectionProps.putAll( connProperties );
    	
    	this.session = session;
    	
    	this.session.getEngine( ).addShutdownListener( new ShutdownListener( session ));
    	
	}
    private class ShutdownListener implements IShutdownListener
    {
    	private DataEngineSession session;
    	
    	public ShutdownListener( DataEngineSession session )
    	{
    		this.session = session;
    	}
    	
    	public void dataEngineShutdown( )
		{
			Map<ConnectionProp, Set<CacheConnection>> odaConnectionsMap = DataSource.dataEngineLevelConnectionPool.remove( this.session );
			if ( odaConnectionsMap == null )
				return;

			ConnectionProp connProp = new ConnectionProp( DataSource.this.driverName,
					DataSource.this.connectionProps,
					DataSource.this.appContext );

			Set<CacheConnection> odaConnections = odaConnectionsMap.get( connProp );
			if ( odaConnections == null )
				return;

			for ( CacheConnection conn : odaConnections )
			{
				try
				{
					conn.odaConn.close( );
				}
				catch ( DataException e )
				{
					// TODO Auto-generated catch block
					e.printStackTrace( );
				}
			}
		};
    }
    
    private Set<CacheConnection> getOdaConnections( boolean populateToCache )
	{
		if ( DataSource.dataEngineLevelConnectionPool.get( this.session ) == null )
		{
			if ( populateToCache )
			{
				DataSource.dataEngineLevelConnectionPool.put( this.session,
						new HashMap<ConnectionProp, Set<CacheConnection>>( ) );
			}
			else
			{
				return new HashSet<CacheConnection>();
			}
		}

		Map<ConnectionProp, Set<CacheConnection>> odaConnectionsMap = DataSource.dataEngineLevelConnectionPool.get( this.session );
		ConnectionProp connProp = new ConnectionProp( this.driverName,
				this.connectionProps,
				this.appContext );
		if ( odaConnectionsMap.get( connProp ) == null )
		{
			odaConnectionsMap.put( connProp, new HashSet<CacheConnection>( ) );
		}
		return odaConnectionsMap.get( connProp );
	}<commit_msg>CheckIn:Fix bugzilla bug  Bug 237190 Created Connections will not be closed (mysql, jdbcDriver 5)<commit_after>/*
 *************************************************************************
 * Copyright (c) 2004, 2005 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *  
 *************************************************************************
 */ 
package org.eclipse.birt.data.engine.executor;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.birt.data.engine.api.IShutdownListener;
import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.i18n.ResourceConstants;
import org.eclipse.birt.data.engine.impl.DataEngineSession;
import org.eclipse.birt.data.engine.odaconsumer.Connection;
import org.eclipse.birt.data.engine.odaconsumer.ConnectionManager;
import org.eclipse.birt.data.engine.odaconsumer.PreparedStatement;
import org.eclipse.birt.data.engine.odi.ICandidateQuery;
import org.eclipse.birt.data.engine.odi.IDataSource;
import org.eclipse.birt.data.engine.odi.IDataSourceQuery;

/**
 * Implementation of ODI's IDataSource interface
 */
class DataSource implements IDataSource
{
	private String 		driverName;
    private Map			appContext;
    private Properties	connectionProps = new Properties();
    
    // A pool of open odaconsumer.Connection. Since each connection may support a limited
	// # of statements, we may need to use more than one connection to handle concurrent statements
	// This is a set of OpenConnection

	
	private static Map<DataEngineSession, Map<ConnectionProp, Set<CacheConnection>>> dataEngineLevelConnectionPool = new Hashtable<DataEngineSession, Map<ConnectionProp, Set<CacheConnection>>>( );
	
	// Currently active oda Statements. This is a map from PreparedStatement to OpenConnection
	private HashMap statementMap = new HashMap();
	
	private static String className = DataSource.class.getName();
	private static Logger logger = Logger.getLogger( className ); 

	private DataEngineSession session;
	
	/**
	 * 
	 * @param driverName
	 * @param connProperties
	 * @param session
	 * @param info
	 */
    public DataSource( String driverName, Map connProperties,
			DataEngineSession session )
	{
    	this.driverName = driverName;
    	if ( connProperties != null )
    		this.connectionProps.putAll( connProperties );
    	
    	this.session = session;
    	
    	this.session.getEngine( ).addShutdownListener( new ShutdownListener( session ));
    	
	}
    private class ShutdownListener implements IShutdownListener
    {
    	private DataEngineSession session;
    	
    	public ShutdownListener( DataEngineSession session )
    	{
    		this.session = session;
    	}
    	
    	public void dataEngineShutdown( )
		{
			Map<ConnectionProp, Set<CacheConnection>> odaConnectionsMap = DataSource.dataEngineLevelConnectionPool.remove( this.session );
			if ( odaConnectionsMap == null )
				return;

			for ( Set<CacheConnection> set : odaConnectionsMap.values( ) )
			{
				for ( CacheConnection conn : set )
				{
					try
					{
						conn.odaConn.close( );
					}
					catch ( DataException e )
					{
						// TODO Auto-generated catch block
						e.printStackTrace( );
					}
				}
			}
		};
	}
    
    private Set<CacheConnection> getOdaConnections( boolean populateToCache )
	{
		if ( DataSource.dataEngineLevelConnectionPool.get( this.session ) == null )
		{
			if ( populateToCache )
			{
				DataSource.dataEngineLevelConnectionPool.put( this.session,
						new HashMap<ConnectionProp, Set<CacheConnection>>( ) );
			}
			else
			{
				return new HashSet<CacheConnection>();
			}
		}

		Map<ConnectionProp, Set<CacheConnection>> odaConnectionsMap = DataSource.dataEngineLevelConnectionPool.get( this.session );
		ConnectionProp connProp = new ConnectionProp( this.driverName,
				this.connectionProps,
				this.appContext );
		if ( odaConnectionsMap.get( connProp ) == null )
		{
			odaConnectionsMap.put( connProp, new HashSet<CacheConnection>( ) );
		}
		return odaConnectionsMap.get( connProp );
	}
{'fraction_non_alphanumeric': 0.12103935971932903, 'fraction_numerical': 0.003837298541826554, 'mean_word_length': 3.995618838992333, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6724293', 'n_tokens_mistral': 2843, 'n_tokens_neox': 2534, 'n_words': 614}