/[AnywhereTS-MSSQL]/trunk/AnywhereTS.DBSupport/DBConnector.cs
ViewVC logotype

Diff of /trunk/AnywhereTS.DBSupport/DBConnector.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/AnywhereTS.DBSupport/DBConnector.cs	2012/07/15 03:09:38	132
+++ trunk/AnywhereTS.DBSupport/DBConnector.cs	2012/07/15 05:00:59	133
@@ -16,9 +16,13 @@
         where DBConnection : DbConnection, new()
         where DBDataAdapter : DbDataAdapter, new()
     {
+        bool ConnectionIsCreated { get; }
         bool ConnectionIsOpen { get; }
-        DBConnection CreateConnection(out Exception ErrorInfo);
+        void CreateConnection(out Exception ErrorInfo);
         void OpenConnection(out Exception ErrorInfo);
+
+        void GetConnectionClone(out DBConnection connection, out Exception ErrorInfo);
+
         void CloseConnection(out Exception ErrorInfo);
 
         DbDataReader ExecuteQuery(string command, List<DBParameter> Params, out Exception ErrorInfo);
@@ -61,6 +65,9 @@
         private string SafeSqlLiteral(string inputSQL) { return inputSQL.Replace("'", "''"); }
         #endregion
         #region IDBConnector members
+        #region  public virtual bool ConnectionIsCreated
+        public virtual bool ConnectionIsCreated { get; protected set; }
+        #endregion
         #region  public virtual bool ConnectionIsOpen
         public virtual bool ConnectionIsOpen { get; protected set; }
         #endregion
@@ -97,25 +104,40 @@
         //    }
         //}
         #endregion
+        #region public virtual void GetConnectionClone(ref DBConnection connection)
+        public virtual void GetConnectionClone(out DBConnection connection, out Exception ErrorInfo)
+        {
+            ErrorInfo = null;
+            connection = this.connection;
+        }
+        #endregion
         #region  public virtual DBConnection CreateConnection(out Exception ErrorInfo)
-        public virtual DBConnection CreateConnection(out Exception ErrorInfo)
+        public virtual void CreateConnection(out Exception ErrorInfo)
         {
             ErrorInfo = null;
             string connetionString = string.Empty;
             try
-            {                
-                connetionString = GetConnectionString();
-                using (log4net.NDC.Push(string.Format("connetionString={0}", connetionString)))
+            {
+                if (!this.ConnectionIsCreated)
+                {
+                    connetionString = GetConnectionString();
+                    using (log4net.NDC.Push(string.Format("connetionString={0}", connetionString)))
+                    {
+                        Logging.DatabaseLog.Debug("Creating Connection");
+                        connection = new DBConnection();
+                        connection.ConnectionString = connetionString;
+                        Logging.DatabaseLog.Debug("Created Connection");
+                        this.ConnectionIsCreated = true;
+                    }
+                }
+                else
                 {
-                    Logging.DatabaseLog.Debug("Creating Connection");
-                    connection = new DBConnection();
-                    connection.ConnectionString = connetionString;
-                    Logging.DatabaseLog.Debug("Created Connection");
+                    throw new Exception("Connection has already been created.");
                 }
-                return this.connection;
             }
             catch (SqlException ex)
             {
+                this.ConnectionIsCreated = false;
                 using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", ex.Number.ToString(), ex.Message, System.Environment.NewLine, ex.ToString())))
                 {
                     Logging.DatabaseLog.Error(string.Format("Failed to create connection to {0} Database", DBDatabase));
@@ -124,6 +146,7 @@
             }
             catch (Exception ex)
             {
+                this.ConnectionIsCreated = false;
                 using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))
                 {
                     Logging.DatabaseLog.Error(string.Format("Failed to create connection to {0} Database", DBDatabase));
@@ -139,23 +162,26 @@
             string connetionString = string.Empty;
             try
             {
-                //this.CloseConnection(out ErrorInfo);
+                if (this.ConnectionIsCreated && this.ConnectionIsOpen)
+                    this.CloseConnection(out ErrorInfo);
+                if (!this.ConnectionIsCreated)
+                    this.CreateConnection(out ErrorInfo);
                 connetionString = GetConnectionString();
                 using (log4net.NDC.Push(string.Format("connectionString={0}", connetionString)))
                 {
                     Logging.DatabaseLog.Debug("Opening Connection");
-                    connection.Open();
-                    this.ConnectionIsOpen = true;
+                    connection.Open();  
+                    this.ConnectionIsOpen = true; 
                     Logging.DatabaseLog.Debug("Opened Connection");
                 }
             }
             //catch (SqlException ex) { Console.WriteLine(ex.ToString()); ErrorInfo = ex;  throw ErrorInfo;}
             catch (SqlException ex)
             {
+                this.ConnectionIsOpen = false;
                 if (ex.Message.ToLower().Contains(string.Format("Cannot open database").ToLower()))
                 {
                     ErrorInfo = null;
-                    return;
                 }
                 using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", ex.Number.ToString(), ex.Message, System.Environment.NewLine, ex.ToString())))
                 {
@@ -165,10 +191,10 @@
             }
             catch (Exception ex)
             {
+                this.ConnectionIsOpen = false;
                 if (ex.Message.ToLower().Contains(string.Format("Cannot open database").ToLower()))
                 {
                     ErrorInfo = null;
-                    return;
                 }
                 using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))
                 {
@@ -185,7 +211,12 @@
             try
             {
                 if (this.ConnectionIsOpen)
+                {
                     connection.Close();
+                    this.ConnectionIsOpen = false;
+                    this.ConnectionIsCreated = false;
+                    this.connection = null;
+                }
             }
             catch (SqlException ex)
             {

 

  ViewVC Help
Powered by ViewVC 1.1.22