I'd like to use try-with-resources for accessing my database as my code has reached a level of complexity where it's difficult to always remember to close the database when moving between threads - I'd rather use try-with-resources.
I use the Base class, as it's advised to use this for cases where you have just one database. (http://javalite.io/database_connection_management, 'DB and Base classes' heading)
I found the documentation where it is described you can simply:
try(DB db = new DB()){
db.open();
// Wrong class, I should be using Base
}
However, unfortunately, this is only the case for the DB class, which it's not advised I use.
In the stack overflow reply here it's said I can write:
try(DB db = Base.open(...)){
// Type error
}
However, this gives IDE error "Incompatible types. Required DB, Found Connection". If I cast Base, this results in "org.sqlite.SQLiteConnection cannot be cast to org.javalite.activejdbc.DB"
try(DB db = (DB) Base.open(...)){
// ClassCastException, you can't cast Base to DB
}
I've also tried keeping it as a Connection, however, when the code block completes it doesn't close the resource, so I get error "Cannot open a new connection because existing connection is still on current thread, name: default"
try(Connection conn = Base.open(...)){
// Doesn't close resource after completing code block
}
Is it possible to use try-with-resources with ActiveJDBC's Base class?