/// <summary> /// Registers/adds new user to database. /// </summary> /// <param name="userID">User id for account creation/login purposes as specified by user.</param> /// <param name="password">Password as specified by user.</param> /// <param name="fullname">Name as specified by user.</param> /// <param name="address">Address as specified by user.</param> /// <param name="email">Email as specified by user.</param> /// <param name="creditcard">Credit card number as specified by user.</param> /// <param name="openBalance">Open balance as specified by user. </param> public AccountDataModel register(string userID, string password, string fullname, string address, string email, string creditcard, decimal openBalance) { //Switch is two let you configure which transaction model you want to benchmark/test. switch (Settings.TRANSACTION_MODEL) { case (StockTraderUtility.TRANSACTION_MODEL_SYSTEMDOTTRANSACTION_TRANSACTION): { //This short try/catch block is introduced to deal with idle-timeout on SQL Azure //connections. It may not be required in the near future, but as of publication //SQL Azure disconnects idle connections after 30 minutes. While command retry-logic //in the DAL automatically deals with this, when performing a tx, with the BSL handling //tx boundaries, we want to go into the tx with known good connections. The try/catch below //ensures this. try { dalCustomer = Trade.DALFactory.Customer.Create(Settings.DAL); dalCustomer.Open(Settings.TRADEDB_SQL_CONN_STRING); dalCustomer.getSQLContextInfo(); } catch { } finally { dalCustomer.Close(); } System.Transactions.TransactionOptions txOps = new TransactionOptions(); txOps.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted; txOps.Timeout = TimeSpan.FromSeconds((double)Settings.SYSTEMDOTTRANSACTION_TIMEOUT); //Start our System.Transactions tx with the options set above. System.Transactions //will handle rollbacks automatically if there is an exception; note the //difference between the System.Transaction case and the ADO.NET transaction case; //and where the dal.Open() happens (which opens a 'hidden' DB connection in DAL). //System.Transactions will automatically enlist ANY connection //opened within the tx scope in the transaction for you. Since it supports distributed //tx's; it frees you quite a bit, with the caveat of the overhead of doing a distributed //tx when you do not need one. Hence: lightweight System.Transactions with an auto-promote to DTC //only if needed, meaning two or more operations use database connections spanning physicall different databases. //ADO.NET txs always require an already-open connection before starting a tx, and txs cannot span multiple databases, just tables. using (TransactionScope tx = new TransactionScope(TransactionScopeOption.Required, txOps)) { //Now open the connection, after entering tx scope. dalCustomer.Open(Settings.TRADEDB_SQL_CONN_STRING);; try { AccountDataModel newCustomer = addNewRegisteredUser(userID, password, fullname, address, email, creditcard, openBalance); //Scope complete, commit work. tx.Complete(); return newCustomer; } catch { //no rollback needed, infrastructure will never commit without //scope.Complete() and immediately issue rollback on and unhandled //exception. throw; } finally { dalCustomer.Close(); } } } case (StockTraderUtility.TRANSACTION_MODEL_ADONET_TRANSACTION): { //ADO.NET TX case: First you need to open the connecton. dalCustomer.Open(Settings.TRADEDB_SQL_CONN_STRING);; //Now you start TX dalCustomer.BeginADOTransaction(); try { AccountDataModel newCustomer = addNewRegisteredUser(userID, password, fullname, address, email, creditcard, openBalance); //done, commit. dalCustomer.CommitADOTransaction(); return newCustomer; } catch { //explicit rollback needed. dalCustomer.RollBackTransaction(); throw; } finally { //ALWAYS call dal.Close is using StockTrader DAL implementation; //this is equivalent to calling Connection.Close() in the DAL -- //but for a generic DB backend as far as the BSL is concerned. dalCustomer.Close(); } } } throw new Exception(Settings.ENABLE_GLOBAL_SYSTEM_DOT_TRANSACTIONS_CONFIGSTRING + ": " + StockTraderUtility.EXCEPTION_MESSAGE_INVALID_TXMMODEL_CONFIG + " Repository ConfigKey table."); }