B4J Question Which method of connecting to the database is optimal?

behnam_tr

Active Member
Licensed User
Longtime User
hi.
Which method of connecting to the database is optimal?
My focus is on memory usage.

1-Persistent connection
Connects to the database from the start of the program and does not close until the end of the program.


B4X:
'mainform
public SQL1 as SQL

sub start
    SQL1.InitializeSQLite("", DBFullPath , False)
end sub

sub mainform_closed
  SQL1.close
end sub
--------------------------------------------
'use in other classes and modules
   main.SQL1.ExecNonQuery(......)

2- Intermittent connection
Connect when needed and disconnect immediately after operation

B4X:
'creat class DBManageClass
private SQL1 as SQL

sub Initialize
   SQL1.InitializeSQLite("", DBFullPath , False)
end sub

sub ExQuery()
 ...............
end sub

sub disconnect
   SQL1.close
end sub
--------------------------------------------

'use in other classes and modules
   dim c1 as DBManageClass
   c1.Initialize
   c1.ExQuery(......)
   c1.disconnect
  
   dim c2 as DBManageClass
   c2.Initialize
   c2.ExQuery(......)
   c2.disconnect
 

tchart

Well-Known Member
Licensed User
Longtime User
#2 but with a connection pool. When you use a pool you open and close the connection immediately and it goes back into the pool. This means there is no delay when reopening a connection.

#1 is fine but limits you to a single connection, this is okay for some apps but not where there are multiple users.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…