B4J Question [SOLVED] What happens to connection pool if the application crashes?

jroriz

Active Member
Licensed User
Longtime User
In the following scenario, a remote MySQL database is accessed via ConnectionPool.

What happens to ConnectionPool when the application crashes without sending pool.ClosePool?

Can the connection get "stuck" causing a server problem?

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
    #AdditionalJar: mysql-connector-java-5.1.48.jar
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim pool As ConnectionPool

#Region Database Location
    Private DBLocation As String = "999.999.999.99:3306"
    Private DBname As String = "dbname"
    Private DBUsername As String = "dbusername"
    Private DBPassword As String = "dbpassword"

#End Region

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    
    MainForm.Initialize("Form", 300,300)
    MainForm.Show
    
    pool.Initialize("com.mysql.jdbc.Driver", $"jdbc:mysql://${DBLocation}/${DBname}?characterEncoding=utf8"$, DBUsername, DBPassword)

    '...
End Sub

Private Sub Form_Closed()
    pool.ClosePool
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    pool.ClosePool
    Return True
End Sub
 

drgottjr

Expert
Licensed User
Longtime User
without a Close(), the connection would eventually be garbage collected. worst case: the server's connection-pool plays tricks with the server. if the pool gets saturated (or a counter loses the correct count), that would make connecting difficult. one would hope the server were implemented in such a way that it would just refuse connections, not crash. how many users of your app are crashing per minute?
 
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
without a Close(), the connection would eventually be garbage collected. worst case: the server's connection-pool plays tricks with the server. if the pool gets saturated (or a counter loses the correct count), that would make connecting difficult. one would hope the server were implemented in such a way that it would just refuse connections, not crash. how many users of your app are crashing per minute?
Crash does not occur often. I refer to everyday situations that occur on the client side, such as a power outage or a connection loss.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
you're not going to bring the server down. you're not the first or only person to lose a connection. the connection stays open on the server side for a bit, then it's marked as inactive and returned to the pool. you would need a bot farm of thousands of phones running your app to crash or exit the app without closing the connection at around the same time and, even then, the server is likely to refuse connections until somebody noticed.
 
Upvote 0
Top