From 680231d7e79cae2636e9722ca12b79345fa2dbdd Mon Sep 17 00:00:00 2001 From: Melanie Thielker Date: Thu, 23 Mar 2017 23:00:48 +0000 Subject: Make the MySqlGeneric layer transaction aware --- OpenSim/Data/MySQL/MySQLFramework.cs | 75 ++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 25 deletions(-) (limited to 'OpenSim/Data/MySQL/MySQLFramework.cs') diff --git a/OpenSim/Data/MySQL/MySQLFramework.cs b/OpenSim/Data/MySQL/MySQLFramework.cs index 34791cf..93662db 100644 --- a/OpenSim/Data/MySQL/MySQLFramework.cs +++ b/OpenSim/Data/MySQL/MySQLFramework.cs @@ -36,7 +36,7 @@ using MySql.Data.MySqlClient; namespace OpenSim.Data.MySQL { /// - /// A database interface class to a user profile storage system + /// Common code for a number of database modules /// public class MySqlFramework { @@ -44,14 +44,24 @@ namespace OpenSim.Data.MySQL log4net.LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); - protected string m_connectionString; - protected object m_dbLock = new object(); + protected string m_connectionString = String.Empty; + protected MySqlTransaction m_trans = null; + // Constructor using a connection string. Instances constructed + // this way will open a new connection for each call. protected MySqlFramework(string connectionString) { m_connectionString = connectionString; } + // Constructor using a connection object. Instances constructed + // this way will use the connection object and never create + // new connections. + protected MySqlFramework(MySqlTransaction trans) + { + m_trans = trans; + } + ////////////////////////////////////////////////////////////// // // All non queries are funneled through one connection @@ -59,33 +69,48 @@ namespace OpenSim.Data.MySQL // protected int ExecuteNonQuery(MySqlCommand cmd) { - lock (m_dbLock) + if (m_trans == null) { using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) { - try - { - dbcon.Open(); - cmd.Connection = dbcon; + dbcon.Open(); + return ExecuteNonQueryWithConnection(cmd, dbcon); + } + } + else + { + return ExecuteNonQueryWithTransaction(cmd, m_trans); + } + } - try - { - return cmd.ExecuteNonQuery(); - } - catch (Exception e) - { - m_log.Error(e.Message, e); - m_log.Error(Environment.StackTrace.ToString()); - return 0; - } - } - catch (Exception e) - { - m_log.Error(e.Message, e); - return 0; - } + private int ExecuteNonQueryWithTransaction(MySqlCommand cmd, MySqlTransaction trans) + { + cmd.Transaction = trans; + return ExecuteNonQueryWithConnection(cmd, trans.Connection); + } + + private int ExecuteNonQueryWithConnection(MySqlCommand cmd, MySqlConnection dbcon) + { + try + { + cmd.Connection = dbcon; + + try + { + return cmd.ExecuteNonQuery(); } + catch (Exception e) + { + m_log.Error(e.Message, e); + m_log.Error(Environment.StackTrace.ToString()); + return 0; + } + } + catch (Exception e) + { + m_log.Error(e.Message, e); + return 0; } } } -} \ No newline at end of file +} -- cgit v1.1