I've had problems for ever with this. See This Post.
My SQL Object is declared as Public in Main.
Various Timers in Main constantly Tick, using the SQL Object, no problem. It never fails when called from Main.
When I make any reference to the use it within a Handler class, the handler doesn't crash, but hangs.
Logs:
Before
True
And nothing else. No errors, and the page hangs.
The problem goes away in Debug mode (single thread) which is a clue, but not much help.
I sort of solved it a year ago by having SQL Objects in the Handler Class, but Erel said that this would corrupt the Database. I have noticed occasional database errors so I guess this is not the right solution.
Having spent the last three days experimenting, I've discovered something that might help Erel or another genius solve this for me:
I can make the Handler pages run by simply Initialising the Main.EnergyDB object again in a Handler:
Logs:
Start
ReInitialized
Queried
What's interesting, is that once I have served any page that has this 'ReInitialize" statement, all of the other Handlers now work, whether they have this statement or not.
My thinking is that the SQL Object in Main has to be Initialized in Main, so that Main has reference to the Database file, then it needs to be Initialized for Jetty, so that Jetty has a reference to the SQLite file, then all is well.
I'm interested to know if my latest 'fix' is likely to cause problems? And if anyone understands what's really going on?
My SQL Object is declared as Public in Main.
B4X:
Process Globals
Public EnergyDB as SQL
AppStart
EnergyDB.InitializeSQLite(...)
Various Timers in Main constantly Tick, using the SQL Object, no problem. It never fails when called from Main.
When I make any reference to the use it within a Handler class, the handler doesn't crash, but hangs.
B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
...
Log("Before")
Log(Main.EnergyDB.IsInitialized)
Dim Hours as ResultSet = Main.EnergyDB.ExecQuery("SELECT * FROM EnergyHours")
Log("After")
Logs:
Before
True
And nothing else. No errors, and the page hangs.
The problem goes away in Debug mode (single thread) which is a clue, but not much help.
I sort of solved it a year ago by having SQL Objects in the Handler Class, but Erel said that this would corrupt the Database. I have noticed occasional database errors so I guess this is not the right solution.
Having spent the last three days experimenting, I've discovered something that might help Erel or another genius solve this for me:
I can make the Handler pages run by simply Initialising the Main.EnergyDB object again in a Handler:
B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
...
Log("Start")
Main.EnergyDB.InitializSQLite(...)
Log("ReInitialized")
Main.EnergyDB.ExecQuery("SELECT * FROM EnergyHours")
Log("Queried")
Logs:
Start
ReInitialized
Queried
What's interesting, is that once I have served any page that has this 'ReInitialize" statement, all of the other Handlers now work, whether they have this statement or not.
My thinking is that the SQL Object in Main has to be Initialized in Main, so that Main has reference to the Database file, then it needs to be Initialized for Jetty, so that Jetty has a reference to the SQLite file, then all is well.
I'm interested to know if my latest 'fix' is likely to cause problems? And if anyone understands what's really going on?