Welcome!

Welcome to the Official BlackBerry Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Developer
jtp5120
Posts: 298
Registered: ‎05-02-2010
My Carrier: Verizon

RuntimeStore + SQLite connection + Singleton == Bad idea?

[ Edited ]

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;
}
}

 

 

--Todd

Windows 7 Enterprise 64-bit (6.1 Build 7600) | Java SE Runtime Environment (build 1.6.0_24-b07) | Eclipse Version: 3.6.2 [M20110210-1200] | BlackBerry Eclipse Plug-in: 1.3.0.201102031007-19 | Java Compiler level: 1.3 | Targeting devices running OS 5 | Simulators: JDE 5.0 packaged 9700, 9630, 9300
Please use plain text.
Developer
traigo
Posts: 79
Registered: ‎11-10-2009
My Carrier: AT&T

Re: RuntimeStore + SQLite connection + Singleton == Bad idea?

What happened with this? Did you decide to do it this way? I'm trying something similar.
Please use plain text.
New Developer
ufoloko
Posts: 27
Registered: ‎09-19-2008

Re: RuntimeStore + SQLite connection + Singleton == Bad idea?

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

Please use plain text.
Developer
Eugen
Posts: 466
Registered: ‎07-16-2009
My Carrier: Vodafone NL

Re: RuntimeStore + SQLite connection + Singleton == Bad idea?

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).

Please use plain text.
New Developer
ufoloko
Posts: 27
Registered: ‎09-19-2008

Re: RuntimeStore + SQLite connection + Singleton == Bad idea?

Maybe you have some example to share ??

Please use plain text.
Developer
Eugen
Posts: 466
Registered: ‎07-16-2009
My Carrier: Vodafone NL

Re: RuntimeStore + SQLite connection + Singleton == Bad idea?

[ Edited ]

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.

Please use plain text.