[성현모] DB Connection 수정

This commit is contained in:
SHM
2025-04-22 14:47:12 +09:00
parent bf5c87ad34
commit 6a3c10c958
3 changed files with 97 additions and 80 deletions

View File

@ -1,11 +1,14 @@
using DB.HubXDB;
using HubX.Library.Config;
using HubX.Library.Socket.Session;
using HubX.Server;
using HubX.Server.Services;
using HubX.Server.TaskManager;
using Microsoft.EntityFrameworkCore;
using System.Net;
using System.Net.Sockets;
using SystemX.Core.Communication;
using SystemX.Core.DB;
using SystemX.Core.Services;
string configDir = @"../Config";
@ -49,6 +52,8 @@ else
return;
}
builder.Services.AddDbContext<HubXContext>();
var app = builder.Build();
//read api config and set

View File

@ -3,7 +3,9 @@ using HubX.Library.Enums;
using HubX.Library.Http.Packet;
using Microsoft.EntityFrameworkCore;
using Microsoft.Identity.Client.Extensions.Msal;
using System;
using System.Data;
using System.Xml;
using SystemX.Core.DB;
namespace HubX.Server.Services
@ -11,10 +13,12 @@ namespace HubX.Server.Services
public class UniqueKeyService
{
private readonly EFCoreService _efCoreService;
private readonly IServiceScopeFactory _scopeFactory;
public UniqueKeyService(EFCoreService efCoreService)
public UniqueKeyService(EFCoreService efCoreService, IServiceScopeFactory scopeFactory)
{
_efCoreService = efCoreService;
_scopeFactory = scopeFactory;
}
public async Task<Response_InsertUniqueKy> Request_InsertUniqueKey(Request_InsertUniqueKey request)
@ -26,10 +30,12 @@ namespace HubX.Server.Services
response.Identity = request.Identity;
bool transactionResult = true;
var context = _efCoreService.GetDBContext<HubXContext>();
using (var scope = _scopeFactory.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<HubXContext>();
if (context != null)
{
var data = context.TStorages.Where(x=>x.CIdentity == request.Identity);
var data = context.TStorages.AsNoTracking().Where(x => x.CIdentity == request.Identity).ToList();
if (data?.Count() > 0)
{
Log4net.WriteLine($"Exist Unique Key", LogType.Error);
@ -48,7 +54,6 @@ namespace HubX.Server.Services
CDateTime = DateTime.Now
};
using (var transaction = await context.CreateTransactionAsync())
{
await context.AddAsync(storage);
@ -68,6 +73,7 @@ namespace HubX.Server.Services
}
}
}
}
return response;
}
@ -79,17 +85,18 @@ namespace HubX.Server.Services
if (request != null)
{
response.Identity = request.Identity;
var context = _efCoreService.GetDBContext<HubXContext>();
using (var scope = _scopeFactory.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<HubXContext>();
if (context != null)
{
try
{
using (var transaction = await context.CreateTransactionAsync(IsolationLevel.ReadUncommitted))
{
var data = context.TStorages.AsNoTracking().First(x=>x.CIdentity == request.Identity);
var data = context.TStorages.AsNoTracking().ToList().First(x => x.CIdentity == request.Identity);
await context.CloseTransactionAsync(transaction);
if(data != null)
if (data != null)
{
response.Data1 = data.CData1;
response.Data2 = data.CData2;
@ -106,6 +113,7 @@ namespace HubX.Server.Services
}
}
}
}
return response;
}
@ -119,7 +127,10 @@ namespace HubX.Server.Services
response.Identity = request.Identity;
bool transactionResult = true;
var context = _efCoreService.GetDBContext<HubXContext>();
using (var scope = _scopeFactory.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<HubXContext>();
if (context != null)
{
var selected = context.TStorages.First(x => x.CIdentity == request.Identity);
@ -150,6 +161,7 @@ namespace HubX.Server.Services
Log4net.WriteLine($"Transaction Success", LogType.DB);
}
}
}
return response;
}