[성현모] 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.Config;
using HubX.Library.Socket.Session; using HubX.Library.Socket.Session;
using HubX.Server; using HubX.Server;
using HubX.Server.Services; using HubX.Server.Services;
using HubX.Server.TaskManager; using HubX.Server.TaskManager;
using Microsoft.EntityFrameworkCore;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using SystemX.Core.Communication; using SystemX.Core.Communication;
using SystemX.Core.DB;
using SystemX.Core.Services; using SystemX.Core.Services;
string configDir = @"../Config"; string configDir = @"../Config";
@ -49,6 +52,8 @@ else
return; return;
} }
builder.Services.AddDbContext<HubXContext>();
var app = builder.Build(); var app = builder.Build();
//read api config and set //read api config and set

View File

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