[Singleton] Class SQL qui fait planté le serveur
Bonjour,
Je me trouve devant un problème que je n'arrive pas a résoudre depuis quelques mois.
J'ai une classe SQL (ci-dessous), je l'ai écrite selon un design pattern singleton.
Mais le problème c'est qu'on dirait qu'au bout de 5 construction d'objet le serveur plante.
Je pense que c'est parce qu'il ne me détruit pas mon objet a la fin du script (l'exception est la pour le vérifié), mais il ne le récupère pas lors de l'appel d'une autre page, donc j'ai plusieurs connexion SQL simultané et puis ca doit planté a cause de ça.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Public Class SQL
Private Shared instance As SQL = Nothing
Private strConnection As String = Nothing
Private connection As SqlConnection = Nothing
Private errorMessage As String = Nothing
Public Shared Function getInstance(ByVal strconnect As String) As SQL
If instance Is Nothing Then
instance = New SQL(strconnect)
instance.connect()
End If
Return instance
End Function
Public Shared Function getInstance() As SQL
If instance Is Nothing Then
instance = New SQL()
End If
Return instance
End Function
Private Sub New()
Throw New Exception("on fait le new")
Me.strConnection = "Data Source=.\SQLEXPRESS;AttachDbFilename=""C:\Documents and Settings\***\BDD.mdf"";Integrated Security=True;Connect Timeout=30;User Instance=True"
End Sub
Private Sub New(ByVal strConnect As String)
Me.strConnection = strConnect
End Sub
Private Function connect() As Boolean
If Me.strConnection Is Nothing Then
Throw New Exception("La chaine de connexion n'a pas été spécifié")
End If
If Me.connection Is Nothing Then
Try
Me.connection = New SqlConnection(Me.strConnection)
Me.connection.Open()
Catch ex As Exception
System.Diagnostics.Debug.WriteLine(ex.ToString()) 'On écrit dans le stream de débug
Me.errorMessage = ex.Message
Return False
End Try
End If
Return True
End Function
Private Function disconnect() As Boolean
If Not Me.connection Is Nothing Then
Try
Me.connection.Close()
Me.connection.Dispose()
Me.connection = Nothing
Me.errorMessage = Nothing
Catch ex As Exception
System.Diagnostics.Debug.WriteLine(ex.ToString()) 'On écrit dans le stream de débug
Me.errorMessage = ex.Message
Return False
End Try
Return True
End If
Return False
End Function
Public Function getError() As String
Return Me.errorMessage
End Function
Public Function query(ByVal strQuery As String) As ArrayList
'...
Return new ArrayList()
End Function
Public Function execute(ByVal strQuery As String) As Integer
'...
Return 0
End Function
Protected Overrides Sub Finalize()
Throw New Exception("On fait finalize")
Me.disconnect()
Me.strConnection = Nothing
MyBase.Finalize()
End Sub
End Class |