02-08-2011 11:30 AM - edited 02-08-2011 11:31 AM
I don't know if this is a good idea or not, I created the following SQLite connection singleton to put into RuntimeStore. In which my goal is to just get a single database connection from multiple threads within an application.
Such as:
SQLManager sqlm = SQLManager.getInstance(); Database db = sqlm.getConnection();
But, as I stare at this, am I creating a memory leak by leaving an open connection to the SQLite database in the runtime store?
If I am, where would I call RuntimeStore.remove(long id), I assume I could call it when the application exits.
Overall, is this implemented correctly or just a bad idea?
public class SQLManager {
private static SQLManager instance;
private Database db = null;
private static long GUID = 0xa178d3ce564cae69L;
static ResourceBundle resource = ResourceBundle.getBundle(
CoffeeHouseResource.BUNDLE_ID, CoffeeHouseResource.BUNDLE_NAME);
private SQLManager() {
try {
URI userFileURL = URI.create(System
.getProperty("fileconn.dir.memorycard")
+ resource.getString(CoffeeHouseResource.db_path)
+ resource.getString(CoffeeHouseResource.db_name));
db = DatabaseFactory.open(userFileURL);
} catch (MalformedURIException e) {
Logger.logEventError("Get connection: URI: " + e.getMessage());
} catch (ControlledAccessException e) {
Logger.logEventError("Get connection: Controlled Access: "
+ e.getMessage());
} catch (DatabasePathException e) {
Logger.logEventError("Get connection: Database Path: "
+ e.getMessage());
} catch (DatabaseIOException e) {
Logger.logEventError("Get connection: Database IO: "
+ e.getMessage());
} catch (Exception e) {
Logger.logEventError(e.getMessage());
}
}
public static SQLManager getInstance() {
if (instance == null) {
instance = (SQLManager) RuntimeStore.getRuntimeStore().get(GUID);
if (instance == null) {
SQLManager singleton = new SQLManager();
RuntimeStore.getRuntimeStore().put(GUID, singleton);
instance = singleton;
}
}
return instance;
}
public final Database getConnection() {
return db;
}
}
02-01-2012 12:48 PM
02-13-2012 11:34 AM
Something new about this.
It is possible to handle this kind of example, I need to do that to share database access between 2 different applications, accessing to the same database
02-13-2012 12:48 PM
If db is using inside one app I don't see why to use RuntimeStore. Just keep a singelton with connection (Don't forget to close it when app is closed).
02-13-2012 12:51 PM
Maybe you have some example to share ??
02-13-2012 01:25 PM - edited 02-13-2012 01:25 PM
Just an updated code from posts above:
public final class SQLManager {
private static SQLManager instance;
private Database db;
private SQLManager() {
try {
URI userFileURL = URI.create(System
.getProperty("fileconn.dir.memorycard")
+ resource.getString(CoffeeHouseResource.db_path)
+ resource.getString(CoffeeHouseResource.db_name));
db = DatabaseFactory.open(userFileURL);
} catch (MalformedURIException e) {
Logger.logEventError("Get connection: URI: " + e.getMessage());
} catch (ControlledAccessException e) {
Logger.logEventError("Get connection: Controlled Access: "
+ e.getMessage());
} catch (DatabasePathException e) {
Logger.logEventError("Get connection: Database Path: "
+ e.getMessage());
} catch (DatabaseIOException e) {
Logger.logEventError("Get connection: Database IO: "
+ e.getMessage());
} catch (Exception e) {
Logger.logEventError(e.getMessage());
}
}
public static synchronized SQLManager getInstance() {
if (instance == null) {
instance = new SQLManager();
}
return instance;
}
public Database getConnection() {
return db;
}
}
But don't use this class until filesystem on SD card is initialized. Also be aware that sometimes SD card is unmounted (when user connect device to PC to synchronize or charge) and you have to restore back the db connection.