protected Table(WriteTransaction txn, string tableName, DatabaseOpenFlags flags) { Database = txn.Transaction.OpenDatabase(tableName, new DatabaseConfiguration { Flags = flags }); txn.Commit(); }
/// <summary> /// this checks if the new key is still available, /// and makes sure it will not be used as a default key anymore /// </summary> /// <param name="AFieldPartnerKey"></param> /// <param name="AOriginalDefaultKey">this has been previously retrieved from GetNewPartnerKey</param> /// <param name="ANewPartnerKey">the user proposes this key for a new partner; the function can change it and return a valid value, or -1</param> /// <param name="ADataBase"></param> /// <returns>whether or not ANewPartnerKey has a valid new partner key; /// if it cannot be assigned, the function returns false, and ANewPartnerKey is -1 /// </returns> public static bool SubmitNewPartnerKey(System.Int64 AFieldPartnerKey, System.Int64 AOriginalDefaultKey, ref System.Int64 ANewPartnerKey, TDataBase ADataBase = null) { bool ReturnValue = true; TDBTransaction ReadTransaction; TDBTransaction WriteTransaction; Boolean NewTransaction; PPartnerLedgerTable PartnerLedgerDT; TDataBase db = DBAccess.Connect("SubmitNewPartnerKey", ADataBase); System.Int64 CurrentDefaultPartnerKey; if (ANewPartnerKey == AOriginalDefaultKey) { // The user has selected the default ReadTransaction = db.GetNewOrExistingTransaction(IsolationLevel.RepeatableRead, out NewTransaction); try { // Fetch the partner ledger record to update the last key PartnerLedgerDT = PPartnerLedgerAccess.LoadByPrimaryKey(AFieldPartnerKey, ReadTransaction); CurrentDefaultPartnerKey = PartnerLedgerDT[0].PartnerKey + PartnerLedgerDT[0].LastPartnerId + 1; if (ANewPartnerKey != CurrentDefaultPartnerKey) { // Someone else has updated this since, so we will use the new default ANewPartnerKey = CurrentDefaultPartnerKey; } // Now check that this does not exist, and increment until we // find one which does not while (PPartnerAccess.Exists(ANewPartnerKey, ReadTransaction)) { ANewPartnerKey = ANewPartnerKey + 1; } } finally { if (NewTransaction) { ReadTransaction.Rollback(); if (TLogging.DebugLevel >= TLogging.DEBUGLEVEL_TRACE) { Console.WriteLine("TNewPartnerKey.SubmitNewPartnerKey: rolled back own transaction."); } } } PartnerLedgerDT[0].LastPartnerId = (int)(ANewPartnerKey - PartnerLedgerDT[0].PartnerKey); WriteTransaction = db.GetNewOrExistingTransaction(IsolationLevel.Serializable, out NewTransaction); try { PPartnerLedgerAccess.SubmitChanges(PartnerLedgerDT, WriteTransaction); if (NewTransaction) { WriteTransaction.Commit(); } } catch (Exception Exc) { TLogging.Log("An Exception occured during the submission of a new PartnerKey:" + Environment.NewLine + Exc.ToString()); if (NewTransaction) { WriteTransaction.Rollback(); } throw; } } // end of: The user has selected the default else { ReadTransaction = db.GetNewOrExistingTransaction(IsolationLevel.RepeatableRead, out NewTransaction); try { // check if the Partner Key is already being used if (PPartnerAccess.Exists(ANewPartnerKey, ReadTransaction)) { ANewPartnerKey = -1; ReturnValue = false; } } finally { if (NewTransaction) { ReadTransaction.Rollback(); if (TLogging.DebugLevel >= TLogging.DEBUGLEVEL_TRACE) { Console.WriteLine("TNewPartnerKey.SubmitNewPartnerKey: rolled back own transaction."); } } } } if (ADataBase == null) { db.CloseDBConnection(); } return(ReturnValue); }