Beispiel #1
0
CaSession* CaSessionManager::SearchSession(CaSession* pInfo)
{
	CaSession* pObj = NULL;
	int i, nAvail = -1;
	for (i=0; i<m_nMaxSession; i++)
	{
		pObj = m_listObject[i];
		if (!pObj && nAvail == -1)
		{
			nAvail = i;
		}

		if (pObj && pObj->IsReleased() && (*pObj == *pInfo))
			return pObj;
	}

	//
	// Create a new session:
	// Find the first released session and disconnect it.
	for (i=0; i<m_nMaxSession && nAvail ==-1; i++)
	{
		pObj = m_listObject[i];
		if (!pObj)
		{
			nAvail = i;
			break;
		}

		if (pObj && pObj->IsReleased())
		{
			//
			// Disconnected the unused session to free the slot:
			nAvail = i;
			pObj->Disconnect();
			delete pObj;
			m_listObject[i] = NULL;
			break;
		}
	}

	if (nAvail != -1)
	{
		try
		{
			pObj = new CaSession (*pInfo);
			pObj->SetSessionNum (nAvail + m_nSessionStart);
			pObj->Connect();
			m_listObject[nAvail] = pObj;
			return pObj;
		}
		catch(CeSqlException e)
		{
			if (pObj)
				delete pObj;
			throw e;
		}
	}

	return NULL;
}
Beispiel #2
0
CaSession* CaSessionManager::GetSession(CaSession* pInfo)
{
	//
	// First of all, check if the session number exceeds the limit.
	// If so, try to destroy those extra sessions:
	CaSession* pObj = NULL;
	int i, nSize = m_listObject.GetSize();
	if (nSize > m_nMaxSession)
	{
		for (i=(nSize -1); i>=m_nMaxSession; i--)
		{
			pObj = m_listObject[i];
			if (pObj && pObj->IsReleased())
			{
				m_listObject[i] = NULL;
				pObj->Disconnect();
				delete pObj;
				pObj = NULL;
			}
		}
	}

	//
	// Search if there is an available session:
	pObj = SearchSession(pInfo);
	if (pObj)
	{
		if (pObj->IsConnected())
			pObj->Activate();
		else
			pObj->Connect();
		return pObj;
	}
	else
	{
		//
		// No session available in the cache and there is no more entry for the new session !
		// All session are used!
		int nExtra = nSize;
		for (i = m_nMaxSession; i < nSize; i++)
		{
			pObj = m_listObject[i];
			if (!pObj)
			{
				nExtra = i;
				break;
			}
		}

		pObj = new CaSession (*pInfo);
		if (nExtra >= nSize)
		{
			nExtra = m_listObject.Add(pObj);
			pObj->SetSessionNum (nExtra + m_nSessionStart);
		}
		else
		{
			pObj->SetSessionNum (nExtra + m_nSessionStart);
			m_listObject.SetAt(nExtra, pObj);
		}
		pObj->Connect();
		return pObj;
	}

	return NULL;
}