The code of TDBACore.vb
:
Imports System.ComponentModel
Imports System.Data
<EditorBrowsable(EditorBrowsableState.Never)> _
Public MustInherit Class TDBACore
Private Shared FRefCount As Integer
Friend Shared FIsBeginTran As Boolean = False
Friend Shared FConnection As IDbConnection
Friend Shared FTransaction As IDbTransaction
Private disposedValue As Boolean
Friend Shared iRecheckTimeout As Integer
Friend Shared iConnectionTimeOut As Integer
Friend MustOverride Function CreateConnection() As IDbConnection
Public Sub New()
Me.disposedValue = False
If TDBACore.FRefCount = 0 Then
TDBACore.FConnection = Me.CreateConnection()
End If
TDBACore.FRefCount += 1
End Sub
Friend Shared Sub OpenConnection(ByVal ConnectionString As String)
If TDBACore.FConnection.State = ConnectionState.Closed Then
TDBACore.FConnection.ConnectionString = ConnectionString
TDBACore.FConnection.Open()
End If
End Sub
End Class
The code of TDBOpener.vb
:
Public NotInheritable Class TDBOpener
Inherits TDBACore
Public Shared ReadOnly _TDBOpener As New TDBOpener()
Friend Overrides Function CreateConnection() As IDbConnection
Return New NpgsqlConnection()
End Function
Public Shared Sub SetTimeoutValue(ByVal param_iRecheckTimeout As Integer, ByVal param_iConnectionTimeOut As Integer)
TDBACore.iConnectionTimeOut = param_iConnectionTimeOut
TDBACore.iRecheckTimeout = param_iRecheckTimeout
End Sub
Public Shared Sub Open(ByVal ConnectionString As String)
TDBACore.OpenConnection(ConnectionString)
End Sub
End Class
I test connect to postgresql by Npgsql.dll 3.2.2.0:
TDBOpener.Open("Server=192.168.1.10;Port=5434;UserId=postgres;Password=123456;Database=testdb;CommandTimeout=300;")
I using Win 10, 64bit, vs 2017 .Net Framework 4.6
If i run by mode [Debug]: it can run function New()
of TDBACore
and connect ok.
But if i run by mode [Release], it not go to function New()
of TDBACore
.
Why? thanks all.(Notes: I try on win 7, it is ok.)
Win 10, 64bit, vs 2017 .Net Framework 4.6:
If i run by mode [Release], it can't init Public Shared ReadOnly _TDBOpener As New TDBOpener()
My solution edit function Open
of TDBOpener
:
Public Shared Sub Open(ByVal ConnectionString As String)
If TDBACore.FConnection Is Nothing Then
Dim obOpen = _TDBOpener
End If
TDBACore.OpenConnection(ConnectionString)
End Sub
The reason you were having the problem, and the reason that the fix works, is because the compiler was optimizing out _TDBOpener
since it wasn't being referenced. This question has more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With