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.

inside custom component

Java Development

Reply
Highlighted
New Contributor
Posts: 2
Registered: ‎11-05-2010
My Device: Not Specified
Accepted Solution

File System error (12)

[ Edited ]

Hey guys. Hope everyone is well!

 

Got a rather annoying problem on my current project. I use a SQLite database for storage of telephone numbers, and this data is used later on to help the user make a phone call. In the main app, these telephone numbers are saved to the sqlite database. Later on, when the user wants to make a phone call (i.e from a menu item I have placed in the phone application), they select my menu item, which loads a popup asking which number to call. The numbers are loaded from the SQLite database, populate a drop down menu, and from there the user can select and call that number.

 

The problem arises when the app is in the background when the user does this. If the app is closed, then the numbers are correctly loaded, but if the app is in the background there are exceptions thrown, both in opening the SQLite database, and when accessing the file system for my custom LogFile.

 

Any assistance in getting to the heart of this problem would be greatly appreciated. I will enclose some code below to give you a better idea of what is going on. I personally think that it is a problem with the files already being accessed by the main app, but I do not know a way of stopping that without bringing the whole thing to its knees as access is still required by the main app.

 

Code:

 

public MyDatabase(String APP_NAME, String DATABASE_NAME)
{
String root = null;
Enumeration e = FileSystemRegistry.listRoots();
while (e.hasMoreElements())
{
root = (String)e.nextElement();
if (root.equalsIgnoreCase("sdcard/"))
{
MyLog.LogInfo("The Device has an SD Card inserted");
}
}
// we now know that the SD card exists
path = "file:///SDCard/BlackBerry/" + APP_NAME + "/Database/" + DATABASE_NAME + ".db";
try
{

myURI = URI.create(path);
FileConnection fileConnection = (FileConnection)Connector.open(path);
if (!fileConnection.exists()) // create the file
{

d = DatabaseFactory.create(myURI);
createTables(d);//This creates the tables
}// if
else
d = DatabaseFactory.open(myURI); // [1]
staticDB = this; //[2]
}// try
catch (Exception ex)
{
staticDB = null;
MyLog.LogError("Database Creation error: " + ex.getMessage()); // [3]
}// catch
}// Database

 

[1] 

*THIS IS WHERE THE CRASH HAPPENS* we get a file system error (12) here. Is the connection already open?

[2]

not called - an exception is thrown before we can do this, but this is used to create a static accessor

[3]

"file system error (12)"

 

The static accessor:

 

 

 

public static MyDatabase getCurrentDatabase()
	{
		if (staticDB == null)
		{
			new MyDatabase(appName, databaseName);
		}
		return staticDB;
	}// getCurrentDatabase

This is in use by the app, so surely staticDB should not be null? But nevertheless, it is null, and a new Database is "created"

 

 

Once again, thanks in advance if you are indeed able to help me with this issue!

 

New Contributor
Posts: 2
Registered: ‎11-05-2010
My Device: Not Specified

Re: File System error (12)

Have solved this one myself. Have enclosed the solution below in case anybody ever has such a problem themselves:

 

 

	public void deactivate()
	{
		MyLog.LogInfo("Deactivated");
		try
		{
			MyDatabase.getCurrentDatabase();
			MyDatabase.closeDB();
		}
		catch (Exception e)
		{
			MyLog.LogError("Could not deactivate");
		}
	}

 This goes in the main Application class, and closes the database when we lose focus (i.e when the application calls deactivate). Here is the closeDB method:

 

 

	public static void closeDB()
	{
		try
		{
			staticDB.d.close();
			staticDB = null;
		}
		catch (DatabaseIOException e)
		{
			MyLog.LogError("Error closing DB " + e.toString());
		}
	}

 I've spent ages working through this with no solution, then posted on here for help and worked out the answer myself within an hour. Typical!

 

Hope this helps anybody out in the future