An unhandled exception of type 'System.TypeInitializationException'
777360Jun 5 2010 — edited Jul 1 2010I'm trying to test a small program, using vc++ and the dot API, based on the ex_txn.cs test program and am getting this error:
*"An unhandled exception of type 'System.TypeInitializationException' occurred in libdb_dotnet50.dll*
*Additional information: The type initializer for 'BerkeleyDB.Internal.libdb_csharpPINVOKE' threw an exception."*
When this line runs:
env = DatabaseEnvironment::Open("c://Temp//TESTDIR", envCfg);
I'm not sure what the problem is( i have a few theories but don't know what to do), full output and code below. Any help would be greatly appreciated. Thanks.
OUTPUT:
'DBtest.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll'
'DBtest.exe' (Managed): Loaded 'c:\Users\peter\Documents\Visual Studio 2008\Projects\DBtest\Debug\DBtest.exe', Symbols loaded.
'DBtest.exe' (Managed): Loaded 'C:\Windows\WinSxS\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.21022.8_none_96748342450f6aa2\msvcm90d.dll', Symbols loaded.
'DBtest.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll'
'DBtest.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Windows.Forms\2.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'
'DBtest.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'
'DBtest.exe' (Managed): Loaded 'c:\Users\peter\Documents\Visual Studio 2008\Projects\DBtest\Debug\libdb_dotnet50.dll', Symbols loaded.
A first chance exception of type 'System.DllNotFoundException' occurred in libdb_dotnet50.dll
A first chance exception of type 'System.TypeInitializationException' occurred in libdb_dotnet50.dll
A first chance exception of type 'System.TypeInitializationException' occurred in libdb_dotnet50.dll
An unhandled exception of type 'System.TypeInitializationException' occurred in libdb_dotnet50.dll
Additional information: The type initializer for 'BerkeleyDB.Internal.libdb_csharpPINVOKE' threw an exception.
The program '[0x1488] DBtest.exe: Managed' has exited with code 0 (0x0).
CODE:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
String^ dbName;
String^ Home;
dbName = "ex_txn.db";
Home = "TESTDIR";
DatabaseEnvironment^ env = nullptr;
Database^ db;
bool inMem;
String^ pwd = Environment::CurrentDirectory;
// Set up the environment.
DatabaseEnvironmentConfig^ envCfg = gcnew DatabaseEnvironmentConfig();
envCfg->Create = true;
envCfg->UseMPool = true;
envCfg->UseLocking = true;
envCfg->UseLogging = true;
envCfg->UseTxns = true;
/*if (inMem)
envCfg->Private = true;
else */
envCfg->RunRecovery = true;
// Set up the database.
BTreeDatabaseConfig^ dbCfg = gcnew BTreeDatabaseConfig();
dbCfg->AutoCommit = true;
dbCfg->Creation = CreatePolicy::IF_NEEDED;
dbCfg->Duplicates = DuplicatesPolicy::SORTED;
dbCfg->FreeThreaded = true;
dbCfg->ReadUncommitted = true;
try {
env = DatabaseEnvironment::Open("c://Temp//TESTDIR", envCfg);
}
catch(DatabaseException ^e) {
MessageBox::Show(e->Message);
}
dbCfg->Env = env;
db = BTreeDatabase::Open(dbName, dbCfg);
/*
* Write a series of records to the database using transaction
* protection. Deadlock handling is demonstrated here.
*/
BinaryFormatter^ formatter = gcnew BinaryFormatter();
MemoryStream^ ms = gcnew MemoryStream();
Random^ generator = gcnew Random();
Transaction^ txn = nullptr;
array<String^>^ keys= {"key 1", "key 2", "key 3", "key 4",
"key 5", "key 6", "key 7", "key 8", "key 9", "key 10"};
int iters = 0;
int retry_count = 0;
int maxRetry = 20;
while (iters < 50)
{
try
{
// Get a transaction.
txn = env->BeginTransaction();
// Write 10 records to the db for each transaction.
for (int j = 0; j < 10; j++)
{
// Get the key.
DatabaseEntry^ key;
key = gcnew DatabaseEntry(
ASCIIEncoding::ASCII->GetBytes(keys[j]));
// Get the data.
/*PayloadData^ pd = gcnew PayloadData(
iters + j,
ThreadCurrentThread::Name,
generator::NextDouble());*/
formatter->Serialize(ms, keys[j]);
array<Byte>^ bytes = ms->GetBuffer();
DatabaseEntry^ data = gcnew DatabaseEntry(bytes);
// Put key/data pair within the transaction.
db->Put(key, data, txn);
}
// // Commit the transaction.
//Console::WriteLine("{0} committing txn: {1}",
// Thread::CurrentThread::Name, iters);
/* int recCount = CountRecords(inMem ? txn : nullptr);
Console.WriteLine("{0} found {1} records in the database.",
Thread::CurrentThread::Name, recCount);*/
try
{
txn->Commit();
txn = nullptr;
}
catch (DatabaseException^ e)
{
Console::WriteLine(
"Error on txn commit: " +
e->ToString());
}
iters++;
retry_count = 0;
}
catch (DatabaseException^ e)
{
// Abort and don't retry.
iters++;
retry_count = 0;
/*Console::WriteLine(Thread::CurrentThread::Name +
" : caught exception: " + e->ToString());
Console.WriteLine(Thread::CurrentThread::Name +
" : errno: " + e->ErrorCode);*/
Console::WriteLine(e->StackTrace);
}
finally
{
if (txn != nullptr)
{
try
{
txn->Abort();
}
catch (DatabaseException^ e)
{
Console::WriteLine("Error aborting transaction: " +
e->ToString());
Console::WriteLine(e->StackTrace);
}
}
}
}
}