code

"열기 / 닫기"SqlConnection 또는 계속 열려 있습니까?

codestyles 2020. 8. 16. 20:22
반응형

"열기 / 닫기"SqlConnection 또는 계속 열려 있습니까?


정적 메서드를 사용하여 간단한 정적 클래스에서 비즈니스 로직을 구현했습니다. 이러한 각 메소드는 호출시 SQL 연결을 열거 나 닫습니다.

public static void DoSomething(string something)
{
    using (SqlConnection connection = new SqlConnection("..."))
    {
        connection.Open();

        // ...

        connection.Close();
    }
}

하지만 연결을 열고 닫지 않으면 성능이 절약 된다고 생각 합니다 . 나는 OleDbConnection 클래스 (SqlConnection에 대해 확실하지 않음)로 오래 전에 몇 가지 테스트를 수행 했으며 확실히 다음과 같이 작동하는 데 도움이되었습니다 (내가 기억하는 한).

//pass the connection object into the method
public static void DoSomething(string something, SqlConnection connection)
{
    bool openConn = (connection.State == ConnectionState.Open);
    if (!openConn)
    {
        connection.Open();
    }

    // ....

    if (openConn) 
    {
        connection.Close();
    }
}

그래서 질문은-방법 (a) 또는 방법 (b)를 선택해야합니까? 연결 풀링이 저에게 성능을 저장했다는 또 다른 stackoverflow 질문을 읽었습니다. 전혀 신경 쓸 필요가 없습니다.

추신. ASP.NET 앱입니다. 연결은 웹 요청 중에 만 존재합니다. 윈앱이나 서비스가 아닙니다.


옵션 a를 고수하십시오 .

연결 풀링은 당신의 친구입니다.


매번 방법 (a)를 사용하십시오. 애플리케이션 확장을 시작할 때 상태를 처리하는 논리는 그렇지 않으면 정말 고통 스러울 것입니다.

연결 풀링은 주석에 표시된대로 수행합니다. 애플리케이션이 확장 될 때 어떤 일이 발생하는지, 연결 열기 / 닫기 상태를 수동으로 관리하는 것이 얼마나 어려울 지 생각해보십시오. 연결 풀은이를 자동으로 처리하는 훌륭한 작업을 수행합니다. 성능이 걱정된다면 어떤 것도 차단되지 않도록 일종의 메모리 캐시 메커니즘을 생각해보십시오.


연결이 끝나면 항상 연결을 닫으십시오. 그러면 기본 데이터베이스 연결이 풀로 돌아가서 다른 호출자가 사용할 수 있습니다. 연결 풀링은 상당히 최적화되어 있으므로 그렇게해도 눈에 띄는 불이익이 없습니다. 조언은 기본적으로 거래의 경우와 동일합니다. 완료되면 짧고 가깝게 유지하세요.

다중 연결을 사용하는 코드 주위에 단일 트랜잭션을 사용하여 MSDTC 문제가 발생하면 실제로 연결 개체를 공유하고 트랜잭션이 완료된 후에 만 ​​닫아야하는 경우 더 복잡해집니다.

그러나 여기서는 작업을 직접 수행하므로 DataSets, Linq to SQL, Entity Framework 또는 NHibernate와 같이 연결을 관리하는 도구를 조사 할 수 있습니다.


면책 조항 : 이것이 오래되었다는 것을 알고 있지만이 사실을 쉽게 입증 할 수있는 방법을 찾았으므로 2 센트 가치를 투자하고 있습니다.

풀링이 실제로 더 빨라질 것이라고 생각하는 데 문제가있는 경우 다음을 시도해보십시오.

어딘가에 다음을 추가하십시오.

using System.Diagnostics;
public static class TestExtensions
{
    public static void TimedOpen(this SqlConnection conn)
    {
        Stopwatch sw = Stopwatch.StartNew();
        conn.Open();
        Console.WriteLine(sw.Elapsed);
    }
}

이제 모든 호출을 Open()바꾸고 TimedOpen()프로그램을 실행하십시오. 이제 각각의 고유 한 연결 문자열에 대해 콘솔 (출력) 창에 하나의 장기 실행 창이 열리고 매우 빠르게 열리게됩니다.

레이블 new StackTrace(true).GetFrame(1) +을 지정하려면에 대한 호출에 추가 할 수 있습니다 WriteLine.


There are distinctions between physical and logical connections. DbConnection is a kind of logical connection and it uses underlying physical connection to Oracle. Closing/opening DbConnection doesn't affect your performance, but makes your code clean and stable - connection leaks are impossible in this case.

Also you should remember about cases when there are limitations for parallel connections on db server - taking that into account it is necessary to make your connections very short.

Connection pool frees you from connection state checking - just open, use and immediately close them.


Normally you should keep one connect for each transaction(no parallel computes)

e.g when user execute charge action, your application need find user's balance first and update it, they should use same connection.

Even if ado.net has its connection pool, dispatching connection cost is very low, but reuse connection is more better choice.

Why not keep only one connection in application

Because the connection is blocking when you execute some query or command, so that means your application is only doing one db operation at sametime, how poor performance it is.

One more issue is that your application will always have a connection even though your user is just open it but no operations.If there are many user open your application, db server will cost all of its connection source in soon while your users have not did anything.

참고URL : https://stackoverflow.com/questions/4439409/open-close-sqlconnection-or-keep-open

반응형