Make the MySqlGeneric layer transaction aware

melanie
Melanie Thielker 2017-03-23 23:00:48 +00:00
parent 3e880cee45
commit 680231d7e7
2 changed files with 159 additions and 87 deletions

View File

@ -36,7 +36,7 @@ using MySql.Data.MySqlClient;
namespace OpenSim.Data.MySQL namespace OpenSim.Data.MySQL
{ {
/// <summary> /// <summary>
/// A database interface class to a user profile storage system /// Common code for a number of database modules
/// </summary> /// </summary>
public class MySqlFramework public class MySqlFramework
{ {
@ -44,14 +44,24 @@ namespace OpenSim.Data.MySQL
log4net.LogManager.GetLogger( log4net.LogManager.GetLogger(
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
protected string m_connectionString; protected string m_connectionString = String.Empty;
protected object m_dbLock = new object(); 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) protected MySqlFramework(string connectionString)
{ {
m_connectionString = 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 // All non queries are funneled through one connection
@ -59,13 +69,30 @@ namespace OpenSim.Data.MySQL
// //
protected int ExecuteNonQuery(MySqlCommand cmd) protected int ExecuteNonQuery(MySqlCommand cmd)
{ {
lock (m_dbLock) if (m_trans == null)
{ {
using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
{
dbcon.Open();
return ExecuteNonQueryWithConnection(cmd, dbcon);
}
}
else
{
return ExecuteNonQueryWithTransaction(cmd, m_trans);
}
}
private int ExecuteNonQueryWithTransaction(MySqlCommand cmd, MySqlTransaction trans)
{
cmd.Transaction = trans;
return ExecuteNonQueryWithConnection(cmd, trans.Connection);
}
private int ExecuteNonQueryWithConnection(MySqlCommand cmd, MySqlConnection dbcon)
{ {
try try
{ {
dbcon.Open();
cmd.Connection = dbcon; cmd.Connection = dbcon;
try try
@ -86,6 +113,4 @@ namespace OpenSim.Data.MySQL
} }
} }
} }
}
}
} }

View File

@ -53,14 +53,27 @@ namespace OpenSim.Data.MySQL
get { return GetType().Assembly; } get { return GetType().Assembly; }
} }
public MySQLGenericTableHandler(MySqlTransaction trans,
string realm, string storeName) : base(trans)
{
m_Realm = realm;
CommonConstruct(storeName);
}
public MySQLGenericTableHandler(string connectionString, public MySQLGenericTableHandler(string connectionString,
string realm, string storeName) : base(connectionString) string realm, string storeName) : base(connectionString)
{ {
m_Realm = realm; m_Realm = realm;
m_connectionString = connectionString;
CommonConstruct(storeName);
}
protected void CommonConstruct(string storeName)
{
if (storeName != String.Empty) if (storeName != String.Empty)
{ {
// We always use a new connection for any Migrations
using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
{ {
dbcon.Open(); dbcon.Open();
@ -110,6 +123,11 @@ namespace OpenSim.Data.MySQL
} }
public virtual T[] Get(string[] fields, string[] keys) public virtual T[] Get(string[] fields, string[] keys)
{
return Get(fields, keys, String.Empty);
}
public virtual T[] Get(string[] fields, string[] keys, string options)
{ {
if (fields.Length != keys.Length) if (fields.Length != keys.Length)
return new T[0]; return new T[0];
@ -126,8 +144,8 @@ namespace OpenSim.Data.MySQL
string where = String.Join(" and ", terms.ToArray()); string where = String.Join(" and ", terms.ToArray());
string query = String.Format("select * from {0} where {1}", string query = String.Format("select * from {0} where {1} {2}",
m_Realm, where); m_Realm, where, options);
cmd.CommandText = query; cmd.CommandText = query;
@ -137,11 +155,32 @@ namespace OpenSim.Data.MySQL
protected T[] DoQuery(MySqlCommand cmd) protected T[] DoQuery(MySqlCommand cmd)
{ {
List<T> result = new List<T>(); if (m_trans == null)
{
using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
{ {
dbcon.Open(); dbcon.Open();
return DoQueryWithConnection(cmd, dbcon);
}
}
else
{
return DoQueryWithTransaction(cmd, m_trans);
}
}
protected T[] DoQueryWithTransaction(MySqlCommand cmd, MySqlTransaction trans)
{
cmd.Transaction = trans;
return DoQueryWithConnection(cmd, trans.Connection);
}
protected T[] DoQueryWithConnection(MySqlCommand cmd, MySqlConnection dbcon)
{
List<T> result = new List<T>();
cmd.Connection = dbcon; cmd.Connection = dbcon;
using (IDataReader reader = cmd.ExecuteReader()) using (IDataReader reader = cmd.ExecuteReader())
@ -204,7 +243,6 @@ namespace OpenSim.Data.MySQL
result.Add(row); result.Add(row);
} }
} }
}
return result.ToArray(); return result.ToArray();
} }
@ -356,6 +394,8 @@ namespace OpenSim.Data.MySQL
} }
public object DoQueryScalar(MySqlCommand cmd) public object DoQueryScalar(MySqlCommand cmd)
{
if (m_trans == null)
{ {
using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
{ {
@ -365,6 +405,13 @@ namespace OpenSim.Data.MySQL
return cmd.ExecuteScalar(); return cmd.ExecuteScalar();
} }
} }
else
{
cmd.Connection = m_trans.Connection;
cmd.Transaction = m_trans;
return cmd.ExecuteScalar();
}
}
} }
} }