Skip to content

Commit f4650e2

Browse files
Configuration of the database session is updated to use a datastore (#829)
* Configuration of the database session is updated to use a datastore instead of separate properties. * Add trace for database and redis session providers. * Fix connection string for session Datastore. * Remove empty lines. (cherry picked from commit b071a83)
1 parent 8515b1c commit f4650e2

File tree

1 file changed

+49
-46
lines changed

1 file changed

+49
-46
lines changed

dotnet/src/dotnetcore/GxClasses/Services/Session/GXSessionFactory.cs

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System;
2+
using GeneXus.Application;
23
using GeneXus.Configuration;
4+
using GeneXus.Data;
5+
using GeneXus.Data.ADO;
36
using GeneXus.Encryption;
47
using GxClasses.Helpers;
58
using log4net;
@@ -11,7 +14,6 @@ public class GXSessionServiceFactory
1114
private static readonly ILog log = log4net.LogManager.GetLogger(typeof(GXSessionServiceFactory));
1215
static string REDIS = "REDIS";
1316
static string DATABASE = "DATABASE";
14-
1517
public static ISessionService GetProvider()
1618
{
1719
ISessionService sessionService = null;
@@ -32,7 +34,6 @@ public static ISessionService GetProvider()
3234
}
3335
else
3436
{
35-
3637
GXLogging.Debug(log, "Loading Session provider:", className);
3738
#if !NETCORE
3839
type = Type.GetType(className, true, true);
@@ -53,16 +54,15 @@ public static ISessionService GetProvider()
5354
}
5455
}
5556
return null;
56-
5757
}
5858
}
5959
public class GxRedisSession : ISessionService
6060
{
61+
private static readonly ILog log = log4net.LogManager.GetLogger(typeof(GxRedisSession));
6162
internal static string SESSION_ADDRESS = "SESSION_PROVIDER_ADDRESS";
6263
internal static string SESSION_INSTANCE = "SESSION_PROVIDER_INSTANCE_NAME";
6364
internal static string SESSION_PASSWORD = "SESSION_PROVIDER_PASSWORD";
6465
static string SESSION_TIMEOUT = "SESSION_PROVIDER_SESSION_TIMEOUT";
65-
6666
public GxRedisSession(GXService serviceProvider)
6767
{
6868
string password = serviceProvider.Properties.Get(SESSION_PASSWORD);
@@ -85,6 +85,8 @@ public GxRedisSession(GXService serviceProvider)
8585
int.TryParse(sessionTimeoutStrCompatibility, out sessionTimeoutMinutes);
8686

8787
SessionTimeout = sessionTimeoutMinutes;
88+
GXLogging.Debug(log, "Redis Host:", host, ", InstanceName:", instanceName);
89+
GXLogging.Debug(log, "Redis sessionTimeoutMinutes:", sessionTimeoutMinutes.ToString());
8890
}
8991
public GxRedisSession(string host, string password, string instanceName, int sessionTimeout)
9092
{
@@ -95,63 +97,69 @@ public GxRedisSession(string host, string password, string instanceName, int ses
9597
}
9698
InstanceName = instanceName;
9799
SessionTimeout = sessionTimeout;
100+
GXLogging.Debug(log, "Redis Host:", host, ", InstanceName:", instanceName);
101+
GXLogging.Debug(log, "Redis sessionTimeoutMinutes:", sessionTimeout.ToString());
98102
}
99103
public string ConnectionString { get; }
100104
public string InstanceName { get; }
101105
public int SessionTimeout { get; }
102-
103106
public string Schema => throw new NotImplementedException();
104-
105107
public string TableName => throw new NotImplementedException();
106108
}
107109
public class GxDatabaseSession : ISessionService
108110
{
111+
private static readonly ILog log = log4net.LogManager.GetLogger(typeof(GxDatabaseSession));
109112
internal static string SESSION_ADDRESS = "SESSION_PROVIDER_ADDRESS";
110113
internal static string SESSION_PASSWORD = "SESSION_PROVIDER_PASSWORD";
111114
internal static string SESSION_SCHEMA = "SESSION_PROVIDER_SCHEMA";
112115
internal static string SESSION_TABLE_NAME = "SESSION_PROVIDER_TABLE_NAME";
113-
internal static string SESSION_PROVIDER_SERVER = "SESSION_PROVIDER_SERVER";
114-
internal static string SESSION_PROVIDER_DATABASE = "SESSION_PROVIDER_DATABASE";
115-
internal static string SESSION_PROVIDER_USER = "SESSION_PROVIDER_USER";
116-
116+
internal static string SESSION_DATASTORE = "SESSION_PROVIDER_DATASTORE";
117+
const string DEFAULT_SQLSERVER_SCHEMA = "dbo";
117118
public GxDatabaseSession(GXService serviceProvider)
118119
{
119-
string password = serviceProvider.Properties.Get(SESSION_PASSWORD);
120-
if (!string.IsNullOrEmpty(password))
121-
{
122-
password = CryptoImpl.Decrypt(password);
123-
}
124-
string serverName = serviceProvider.Properties.Get(SESSION_PROVIDER_SERVER);
125-
string userName = serviceProvider.Properties.Get(SESSION_PROVIDER_USER);
126-
string database = serviceProvider.Properties.Get(SESSION_PROVIDER_DATABASE);
127-
string schema = serviceProvider.Properties.Get(SESSION_SCHEMA);
128-
string tableName = serviceProvider.Properties.Get(SESSION_TABLE_NAME);
129-
130-
string sessionAddresCompatibility = serviceProvider.Properties.Get(GxDatabaseSession.SESSION_ADDRESS);
131-
if (!string.IsNullOrEmpty(sessionAddresCompatibility))
132-
{
133-
ConnectionString = sessionAddresCompatibility;
134-
}
135-
136-
if (!string.IsNullOrEmpty(serverName))
120+
string datastoreName = serviceProvider.Properties.Get(SESSION_DATASTORE);
121+
if (!string.IsNullOrEmpty(datastoreName))
137122
{
138-
ConnectionString += $"Data Source={serverName};";
139-
}
140-
if (!string.IsNullOrEmpty(database))
141-
{
142-
ConnectionString += $"Initial Catalog={database}";
143-
}
144-
if (!string.IsNullOrEmpty(password))
145-
{
146-
ConnectionString += $";password={password}";
123+
GxContext context = GxContext.CreateDefaultInstance();
124+
IGxDataStore datastore = context.GetDataStore(datastoreName);
125+
string schema = datastore.Connection.CurrentSchema;
126+
if (string.IsNullOrEmpty(schema))
127+
schema = DEFAULT_SQLSERVER_SCHEMA;
128+
string tableName = serviceProvider.Properties.Get(SESSION_TABLE_NAME);
129+
GxConnection conn = datastore.Connection as GxConnection;
130+
Schema = schema;
131+
TableName = tableName;
132+
context.CloseConnections();
133+
GxDataRecord dr = datastore.Db as GxDataRecord;
134+
if (dr != null && conn != null)
135+
{
136+
ConnectionString = dr.BuildConnectionStringImpl(conn.DataSourceName, conn.InternalUserId, conn.UserPassword, conn.DatabaseName, conn.Port, conn.CurrentSchema, conn.Data);
137+
GXLogging.Debug(log, "Database ConnectionString:", dr.ConnectionStringForLog());
138+
}
147139
}
148-
if (!string.IsNullOrEmpty(userName))
140+
else //Backward compatibility configuration
149141
{
150-
ConnectionString += $";user={userName}";
142+
string password = serviceProvider.Properties.Get(SESSION_PASSWORD);
143+
if (!string.IsNullOrEmpty(password))
144+
{
145+
password = CryptoImpl.Decrypt(password);
146+
}
147+
string schema = serviceProvider.Properties.Get(SESSION_SCHEMA);
148+
string tableName = serviceProvider.Properties.Get(SESSION_TABLE_NAME);
149+
string sessionAddresCompatibility = serviceProvider.Properties.Get(SESSION_ADDRESS);
150+
if (!string.IsNullOrEmpty(sessionAddresCompatibility))
151+
{
152+
ConnectionString = sessionAddresCompatibility;
153+
}
154+
if (!string.IsNullOrEmpty(password))
155+
{
156+
ConnectionString += $";password={password}";
157+
}
158+
Schema = schema;
159+
TableName = tableName;
151160
}
152-
Schema = schema;
153-
TableName = tableName;
154161
SessionTimeout = Preferences.SessionTimeout;
162+
GXLogging.Debug(log, "Database sessionTimeoutMinutes:", SessionTimeout.ToString());
155163
}
156164
public GxDatabaseSession(string host, string password, string schema, string tableName)
157165
{
@@ -164,16 +172,11 @@ public GxDatabaseSession(string host, string password, string schema, string tab
164172
TableName = tableName;
165173
}
166174
public string ConnectionString { get; }
167-
168175
public string Schema { get; }
169-
170176
public string TableName { get; }
171-
172177
public string InstanceName => throw new NotImplementedException();
173-
174178
public int SessionTimeout { get; }
175179
}
176-
177180
public interface ISessionService
178181
{
179182
string ConnectionString { get; }

0 commit comments

Comments
 (0)