NewLife/X

v9.7.2018.0421   支持运行时修改DAL连接字符串
大石头 authored at 2018-04-21 14:00:47
5b7de4b
Tree
1 Parent(s) 35c1e9f
Summary: 8 changed files with 63 additions and 52 deletions.
Modified +6 -22
Modified +1 -1
Modified +24 -3
Modified +3 -0
Modified +15 -14
Modified +8 -10
Modified +4 -2
Modified +2 -0
Modified +6 -22
diff --git a/NewLife.Cube/Common/ViewHelper.cs b/NewLife.Cube/Common/ViewHelper.cs
index 6a7d01f..01fb0e6 100644
--- a/NewLife.Cube/Common/ViewHelper.cs
+++ b/NewLife.Cube/Common/ViewHelper.cs
@@ -37,18 +37,12 @@ namespace NewLife.Cube
         /// <summary>获取页面设置</summary>
         /// <param name="page"></param>
         /// <returns></returns>
-        public static Bootstrap Bootstrap(this WebViewPage page)
-        {
-            return Bootstrap(page.Context);
-        }
+        public static Bootstrap Bootstrap(this WebViewPage page) => Bootstrap(page.Context);
 
         /// <summary>获取页面设置</summary>
         /// <param name="controller"></param>
         /// <returns></returns>
-        public static Bootstrap Bootstrap(this Controller controller)
-        {
-            return Bootstrap(controller.HttpContext);
-        }
+        public static Bootstrap Bootstrap(this Controller controller) => Bootstrap(controller.HttpContext);
 
         /// <summary>获取路由Key</summary>
         /// <param name="entity"></param>
@@ -86,8 +80,7 @@ namespace NewLife.Cube
 
             return dic;
         }
-
-
+        
         internal static Boolean MakeListDataView(Type entityType, String vpath, List<FieldItem> fields)
         {
             var tmp = @"@using NewLife;
@@ -275,20 +268,11 @@ namespace NewLife.Cube
             return true;
         }
 
-        private static void BuildUser(FieldItem item, StringBuilder sb)
-        {
-            sb.AppendFormat(@"<td class=""text-right"">@provider.FindByID(entity.{0})</td>", item.Name);
-        }
+        private static void BuildUser(FieldItem item, StringBuilder sb) => sb.AppendFormat(@"<td class=""text-right"">@provider.FindByID(entity.{0})</td>", item.Name);
 
-        private static void BuildIP(FieldItem item, StringBuilder sb)
-        {
-            sb.AppendFormat(@"<td title=""@entity.{0}.IPToAddress()"">@entity.{0}</td>", item.Name);
-        }
+        private static void BuildIP(FieldItem item, StringBuilder sb) => sb.AppendFormat(@"<td title=""@entity.{0}.IPToAddress()"">@entity.{0}</td>", item.Name);
 
-        internal static Boolean MakeFormView()
-        {
-            return false;
-        }
+        internal static Boolean MakeFormView() => false;
 
         /// <summary>是否启用多选</summary>
         /// <param name="page"></param>
Modified +1 -1
diff --git a/Test/App.config b/Test/App.config
index b4fc241..d0fb6e4 100644
--- a/Test/App.config
+++ b/Test/App.config
@@ -5,7 +5,7 @@
   <connectionStrings>
     <add name="MSSQL" connectionString="Data Source=.;Initial Catalog=master;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
     <!--<add name="Membership" connectionString="Data Source=.;Initial Catalog=Membership;user id=sa;password=Pass@word" providerName="System.Data.SqlClient" />-->
-    <add name="Membership" connectionString="Data Source=Membership.db;Pooling=true;Cache Size=-524288;Journal Mode=Memory;" providerName="Sqlite" />
+    <!--<add name="Membership" connectionString="Data Source=Membership.db;Pooling=true;Cache Size=-524288;Journal Mode=Memory;" providerName="Sqlite" />-->
     <!--<add name="Membership" connectionString="Data Source=:memory:;Pooling=true;Cache Size=-524288;Journal Mode=Memory;" providerName="Sqlite" />-->
     <!--<add name="Membership" connectionString="Server=.;Port=3306;Database=Membership;Uid=root;Pwd=;" providerName="MySql.Data.MySqlClient"/>-->
     <!--<add name="PostgreSQL" connectionString="Server=.;Database=master;Uid=root;Pwd=root;" providerName="PostgreSQL.Data.PostgreSQLClient" />-->
Modified +24 -3
diff --git a/Test/Program.cs b/Test/Program.cs
index 3e0ace6..027d655 100644
--- a/Test/Program.cs
+++ b/Test/Program.cs
@@ -56,7 +56,7 @@ namespace Test
                 try
                 {
 #endif
-                    Test5();
+                Test5();
 #if !DEBUG
                 }
                 catch (Exception ex)
@@ -187,8 +187,29 @@ namespace Test
 
         static void Test5()
         {
-            JsonConfigTest.Start();
+            //JsonConfigTest.Start();
+
+            var dal = UserX.Meta.Session.Dal;
+            var connStr = "Data Source=Membership222.db";
+            //dal.ConnStr = connStr;
+
+            var user = UserX.FindByName("admin");
+            Console.WriteLine("{0}\t{1}", user, user.RegisterTime);
+            //user.RegisterTime = new DateTime(2018, 1, 1);
+            //user.Save();
+
+            //var connStr = dal.ConnStr;
+            dal.ConnStr = connStr;
+
+            for (var i = 0; i < 1000; i++)
+            {
+                Thread.Sleep(3000);
+
+                user = UserX.FindByName("admin");
+                Console.WriteLine("{0}\t{1}", user, user.RegisterTime);
+                //user.RegisterTime = DateTime.Now;
+                //user.Save();
+            }
         }
     }
-
 }
\ No newline at end of file
Modified +3 -0
diff --git a/XCode/DataAccessLayer/Common/ConnectionPool.cs b/XCode/DataAccessLayer/Common/ConnectionPool.cs
index 1b4d406..5859066 100644
--- a/XCode/DataAccessLayer/Common/ConnectionPool.cs
+++ b/XCode/DataAccessLayer/Common/ConnectionPool.cs
@@ -75,6 +75,9 @@ namespace XCode.DataAccessLayer
         {
             try
             {
+                // 如果连接字符串变了,则关闭
+                if (value.ConnectionString != ConnectionString) value.Close();
+
                 return value.State == ConnectionState.Open;
             }
             catch { return false; }
Modified +15 -14
diff --git a/XCode/DataAccessLayer/Common/DbBase.cs b/XCode/DataAccessLayer/Common/DbBase.cs
index 5516e94..653cdd1 100644
--- a/XCode/DataAccessLayer/Common/DbBase.cs
+++ b/XCode/DataAccessLayer/Common/DbBase.cs
@@ -90,7 +90,7 @@ namespace XCode.DataAccessLayer
 
         #region 属性
         /// <summary>返回数据库类型。外部DAL数据库类请使用Other</summary>
-        public virtual DatabaseType Type { get { return DatabaseType.None; } }
+        public virtual DatabaseType Type => DatabaseType.None;
 
         /// <summary>工厂</summary>
         public abstract DbProviderFactory Factory { get; }
@@ -127,6 +127,13 @@ namespace XCode.DataAccessLayer
 
                     ReleaseSession();
                 }
+
+                // 更新连接池的连接字符串
+                if (_Pool != null)
+                {
+                    _Pool.ConnectionString = connStr;
+                    _Pool.Clear();
+                }
             }
         }
 
@@ -160,7 +167,7 @@ namespace XCode.DataAccessLayer
             }
         }
 
-        protected virtual String DefaultConnectionString { get { return String.Empty; } }
+        protected virtual String DefaultConnectionString => String.Empty;
 
         /// <summary>设置连接字符串时允许从中取值或修改,基类用于读取拥有者Owner,子类重写时应调用基类</summary>
         /// <param name="builder"></param>
@@ -278,7 +285,7 @@ namespace XCode.DataAccessLayer
         /// <summary>是否支持该提供者所描述的数据库</summary>
         /// <param name="providerName">提供者</param>
         /// <returns></returns>
-        public virtual Boolean Support(String providerName) { return !String.IsNullOrEmpty(providerName) && providerName.ToLower().Contains(Type.ToString().ToLower()); }
+        public virtual Boolean Support(String providerName) => !providerName.IsNullOrEmpty() && providerName.ToLower().Contains(Type.ToString().ToLower());
         #endregion
 
         #region 下载驱动
@@ -687,7 +694,7 @@ namespace XCode.DataAccessLayer
         /// <param name="field">字段</param>
         /// <param name="value">数值</param>
         /// <returns></returns>
-        public virtual String FormatIdentity(IDataColumn field, Object value) { return null; }
+        public virtual String FormatIdentity(IDataColumn field, Object value) => null;
 
         /// <summary>格式化参数名</summary>
         /// <param name="name">名称</param>
@@ -704,13 +711,13 @@ namespace XCode.DataAccessLayer
             return ParamPrefix + name;
         }
 
-        internal protected virtual String ParamPrefix { get { return "@"; } }
+        internal protected virtual String ParamPrefix => "@";
 
         /// <summary>字符串相加</summary>
         /// <param name="left"></param>
         /// <param name="right"></param>
         /// <returns></returns>
-        public virtual String StringConcat(String left, String right) { return (!String.IsNullOrEmpty(left) ? left : "\'\'") + "+" + (!String.IsNullOrEmpty(right) ? right : "\'\'"); }
+        public virtual String StringConcat(String left, String right) => (!left.IsNullOrEmpty() ? left : "\'\'") + "+" + (!right.IsNullOrEmpty() ? right : "\'\'");
 
         /// <summary>创建参数</summary>
         /// <param name="name">名称</param>
@@ -791,10 +798,7 @@ namespace XCode.DataAccessLayer
         /// <summary>创建参数数组</summary>
         /// <param name="ps"></param>
         /// <returns></returns>
-        public IDataParameter[] CreateParameters(IDictionary<String, Object> ps)
-        {
-            return ps.Select(e => CreateParameter(e.Key, e.Value)).ToArray();
-        }
+        public IDataParameter[] CreateParameters(IDictionary<String, Object> ps) => ps.Select(e => CreateParameter(e.Key, e.Value)).ToArray();
 
         /// <summary>获取 或 设置 自动关闭。每次使用完数据库连接后,是否自动关闭连接,高频操作时设为false可提升性能。默认true</summary>
         public Boolean AutoClose { get; set; } = true;
@@ -803,10 +807,7 @@ namespace XCode.DataAccessLayer
         #region 辅助函数
         /// <summary>已重载。</summary>
         /// <returns></returns>
-        public override String ToString()
-        {
-            return String.Format("[{0}] {1} {2}", ConnName, Type, ServerVersion);
-        }
+        public override String ToString() => String.Format("[{0}] {1} {2}", ConnName, Type, ServerVersion);
 
         protected static String ResolveFile(String file)
         {
Modified +8 -10
diff --git a/XCode/DataAccessLayer/DAL.cs b/XCode/DataAccessLayer/DAL.cs
index d6a9455..0d27ff0 100644
--- a/XCode/DataAccessLayer/DAL.cs
+++ b/XCode/DataAccessLayer/DAL.cs
@@ -201,16 +201,13 @@ namespace XCode.DataAccessLayer
         /// <param name="connName">连接名</param>
         /// <param name="connStr">连接字符串</param>
         /// <param name="provider">数据库提供者</param>
-        public static void RegisterDefault(String connName, String connStr, String provider)
-        {
-            _defs[connName] = new Tuple<String, String>(connStr, provider);
-        }
+        public static void RegisterDefault(String connName, String connStr, String provider) => _defs[connName] = new Tuple<String, String>(connStr, provider);
         #endregion
 
         #region 属性
         private String _ConnName;
         /// <summary>连接名</summary>
-        public String ConnName { get { return _ConnName; } }
+        public String ConnName => _ConnName;
 
         private Type _ProviderType;
         /// <summary>实现了IDatabase接口的数据库类型</summary>
@@ -249,6 +246,10 @@ namespace XCode.DataAccessLayer
                     _ConnStr = value;
                     _ProviderType = null;
                     _Db = null;
+                    _Tables = null;
+                    _hasCheck = 0;
+                    HasCheckTables.Clear();
+                    _Assembly = null;
 
                     AddConnStr(_ConnName, _ConnStr, null, null);
                 }
@@ -286,7 +287,7 @@ namespace XCode.DataAccessLayer
         }
 
         /// <summary>数据库会话</summary>
-        public IDbSession Session { get { return Db.CreateSession(); } }
+        public IDbSession Session => Db.CreateSession();
         #endregion
 
         #region 连接字符串编码解码
@@ -376,10 +377,7 @@ namespace XCode.DataAccessLayer
         /// <summary>导出模型</summary>
         /// <param name="tables"></param>
         /// <returns></returns>
-        public static String Export(IEnumerable<IDataTable> tables)
-        {
-            return ModelHelper.ToXml(tables);
-        }
+        public static String Export(IEnumerable<IDataTable> tables) => ModelHelper.ToXml(tables);
 
         /// <summary>导入模型</summary>
         /// <param name="xml"></param>
Modified +4 -2
diff --git a/XCode/Properties/AssemblyInfo.cs b/XCode/Properties/AssemblyInfo.cs
index f42f381..0193620 100644
--- a/XCode/Properties/AssemblyInfo.cs
+++ b/XCode/Properties/AssemblyInfo.cs
@@ -39,8 +39,8 @@ using XCode;
 //
 // 可以指定所有这些值,也可以使用“修订号”和“内部版本号”的默认值,
 // 方法是按如下所示使用“*”:
-[assembly: AssemblyVersion("9.6.*")]
-[assembly: AssemblyFileVersion("9.6.2018.0326")]
+[assembly: AssemblyVersion("9.7.*")]
+[assembly: AssemblyFileVersion("9.7.2018.0421")]
 
 /*
  * XCode的重大改进
@@ -58,6 +58,8 @@ using XCode;
  * /
 
 /*
+ * v9.7.2018.0421   支持运行时修改DAL连接字符串
+ * 
  * v9.6.2018.0326   重构权限体系,支持多角色
  * 
  * v9.6.2017.0808   重构正向工程,基于映射表查找数据库字段类型到实体类型的映射
Modified +2 -0
diff --git a/XCode/UpdateInfo.txt b/XCode/UpdateInfo.txt
index 7eccc94..91d6d50 100644
--- a/XCode/UpdateInfo.txt
+++ b/XCode/UpdateInfo.txt
@@ -10,6 +10,8 @@ v2.0 数据架构功能,实体和数据结构双向映射
 v1.2 使用泛型基类
 v1.0 创建XCode
 
+v9.7.2018.0421   支持运行时修改DAL连接字符串
+
 v9.6.2018.0326   重构权限体系,支持多角色
 
 v9.6.2017.0808   重构正向工程,基于映射表查找数据库字段类型到实体类型的映射