Unable to close IDatabase

I have the follwoing code:

if( (pDBService = (IDatabaseService*)gCServiceRequest->GetService(kDatabase)) == NULL )
{throw CannotObtainServiceException();
}

pDBService->GetOpenDatabases(&pDBList);
OutputDebugString(L"\n");
OutputDebugStringA(stringify(pDBList->GetCount()).c_str());
OutputDebugString(L" databases open.");

// Create the new Database
if( (iRet=pDBService->Create(dbPath,modeDefault,&pLitCiterDB)) != kServiceNoErr )
{OutputDebugString(L"Error creating database!!!!!");
	throw ServiceException(iRet);
}

pLitCiterDB->Close();

pDBService->GetOpenDatabases(&pDBList);
OutputDebugString(L"\n");
OutputDebugStringA(stringify(pDBList->GetCount()).c_str());
OutputDebugString(L" databases open.");

 Before the call to create, there are no databases open. After the call to close, there is still 1 database open.

This later becomes troublesome when I try to open a library window in EndNote by doing:

if( (iRet=pUIService->OpenDatabase(dbPath)) != kServiceNoErr )

 This returns kServiceErrDbAlreadyOpen. 

What do I need to do to close this database?

I’ve found out a littlbe bit more about this… It appears that I also can’t close any databases at all…

IDatabaseListPtr pDBList;
IDatabasePtr toRemove;
pDBService->GetOpenDatabases(&pDBList);

OutputDebugString(L"\n");
OutputDebugStringA(stringify(pDBList->GetCount()).c_str());
OutputDebugString(L" databases open initially.");

while (pDBList->GetCount() > 0)	
{
	if( (iRet=pDBList->GetDatabase(0, &toRemove)) != kServiceNoErr )
	{
		OutputDebugString(L"Error getting from list!");
	}
	if( (iRet=toRemove->Close()) != kServiceNoErr)
	{
		OutputDebugString(L"Error closing database!");
	}
	if( (iRet=pDBList->RemoveDatabase(toRemove)) != kServiceNoErr )
	{
		OutputDebugString(L"Error removing database!");
	}
}

pDBService->GetOpenDatabases(&pDBList);
OutputDebugString(L"\n");
OutputDebugStringA(stringify(pDBList->GetCount()).c_str());
OutputDebugString(L" databases open after closing them all.");

Before this bit of code is run, I open EndNote. Then, I open 1 database. After that, I execute this code, which gives the following results:

2 databases open initially.

2 databases open after closing them all.

edit: I just realized something… If I open the Database from within EndNote, then my plugin is not the owner, and I cannot close it this way.

Reading over some notes on this I remembered that a plugin cannot close a database it did not open.  So if the user or another plugin opens a database your plugin cannot close the database.

Yeah… I just realized that probably while you were writing that response… That applies to my example where I opened the library from within EndNote. However, in my original post, I Create it and then attempt to close it immediately within my plugin.

Now that I’ve fixed the threading issue from my other post, I still have problems when I try to create the database and then immediately close it.

I found a fix. I have to manually set all the references to NULL. (or make sure they run out of scope) It doesn’t seem like Close() actually does anything when I call it. Even if pLitCiterDB is the last reference, calling pLitCiterDB->Close() doesn’t close it.