DeleteRecordStore error: record store is still open - midlet

I have a simple problem.
I want to delete a recordStore data.
when I execute the deletion code
I get this error message
javax.microedition.rms.RecordStoreException: deleteRecordStore error: record store is still open at
javax.microedition.rms.RecordStore.deleteRecordStore()
try
{
recordStore2.closeRecordStore() ;
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
recordStore2.deleteRecordStore("recordStore2");
}
catch(Exception e)
{
e.printStackTrace();
}

Oh...
I have know the answer.
I should close my RecordStore into each block of code after finishing of using
it,throughout the program.
try
{
recordStore2.closeRecordStore() ;
}
catch(Exception e)
{
e.printStackTrace();
}

Related

What is strict mode policy violation in Android

public static void write(byte[] aInput, String aOutputFileName, String dirName) {
(new File(dirName)).mkdir();
try {
OutputStream output = null;
try {
output = new BufferedOutputStream(new FileOutputStream(dirName + "/" + aOutputFileName));
output.write(aInput);
} finally {
output.close();
}
} catch (FileNotFoundException ex) {
System.out.println("File not found.");
} catch (IOException ex) {
System.out.println(ex);
}
}
The code above is from a library i'm using and it is supposed to create an output file and write a byte array to it. I checked logcat and saw the Strict Mode Policy violation Write.toDisk. I understand what it is supposed to be for my questions are: (1) Does Strict mode prevent you from doing disk reads and write on the main thread? (2) Does that mean that the file or folder were not actually created? (3) How then would I go about creating a folder or file within my App that doesn't trigger this? (4) What is the recommended way to handle disk read / write off the main ui thread, a real world example would be appreciated
Thanks in Advance
(1) It turns out that Strict mode doesn't actually prevent you from making writes to the disk it just gives a warning. From Android Developer "StrictMode is a developer tool which detects things you might be doing by accident and brings them to your attention so you can fix them". https://developer.android.com/reference/android/os/StrictMode
(2) The files were actually being created it's just that I was just not familiar with writing and reading from disk
(3) There are numerous ways to go about creating files (i) first you get a hold of a file directory to write the file to:
context.getFilesDir()
(ii) then you get an outputstream writer (iii) then you write out the data with the writer
public void makeFile(String filename){
//Create temp file for filename
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(filename));
fos.write(filename.getBytes());//Write the contents of the file to app folder
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
(iv) finally you close the outputstream writer
(4) The recommended way is to use either an AsyncTask or some other background running class like FutureTask or to use Threads or Runnable:
public class DownloadFileThread implements Runnable{
public void run(){
//your code here
}
}

Finding all methods that hasn't log inside try catch with NDepend

I'm refactoring an existing application and I have got a per class instance in the form
private readonly ILog log; //(Log4net, but the namespace isn't
I've got some method in the form
public List<Nofity> GetXXX(string codice)
{
try
{
[code]
}
catch (Exception ex)
{
throw ex;
}
}
I need to find the methods that haven't got
catch (Exception ex)
{
log.Error(ex);
throw ex;
}
Is it possible with NDepend?
I also wish to find all the try catch method that swallow the exception
catch()
{
}
How can I do that?
Thanks

Does StreamingOutput in Jersey need to close?

I have a snippet to download a file from jersey 2.22,
StreamingOutput stream = (OutputStream os) -> {
try {
os.write( get a byte array here);
} catch (Exception e) {
LOGGER.error("Failed to fetch file, id:" + fileName, e);
throw new Exception());
}
};
the above code is working, but i'm wondering if the output stream I'm writing in needs to close.
I searched jersey's source code, in OutboundMessageContext
public void close() {
if(this.hasEntity()) {
try {
OutputStream e = this.getEntityStream();
e.flush();
e.close();
} catch (IOException var10) {
Logger.getLogger(OutboundMessageContext.class.getName()).log(Level.FINE, var10.getMessage(), var10);
} finally {
if(!this.committingOutputStream.isClosed()) {
try {
this.committingOutputStream.close();
} catch (IOException var9) {
Logger.getLogger(OutboundMessageContext.class.getName()).log(Level.FINE, var9.getMessage(), var9);
}
}
}
}
}
this method is to close entity stream. I'm not sure if this is the right close method working with StreamingOutput, can anyone help?

Hibernate Search call to SearchFactory optimize does not invoke immediately

I'm trying to call SearchFactory optimize to run a scheduled index maintenance job (compacting segments - the application is a write intensive). But it does not seem to invoke immediately until I shutdown the Tomcat. My code is calling simply like this.
public synchronized void optimizeIndexes() {
getFullTextEntityManager().flushToIndexes(); //apply any changes before optimizing
getFullTextEntityManager().getSearchFactory().optimize();
logger.info("[Lucene] optimization has performed on all the indexes...");
}
I got it to work around by loaning IndexWriter from HSearch backend.
private synchronized void optimizeBareMetal() {
try {
LuceneBackendQueueProcessor backend = (LuceneBackendQueueProcessor) getIndexManager().getBackendQueueProcessor();
LuceneBackendResources resources = backend.getIndexResources();
AbstractWorkspaceImpl workspace = resources.getWorkspace();
IndexWriter indexWriter = workspace.getIndexWriter();
indexWriter.forceMerge(1, true);
indexWriter.commit();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private synchronized DirectoryBasedIndexManager getIndexManager() {
SearchFactoryImplementor searchFactory = (SearchFactoryImplementor) getFullTextEntityManager().getSearchFactory();
IndexManagerHolder indexManagerHolder = searchFactory.getIndexManagerHolder();
return (DirectoryBasedIndexManager) indexManagerHolder.getIndexManager(getEntityClass().getName());
}

Propogate errors to UI with Spring 3 MVC / REST

When /api/upload REST endpoint is accessed I have a UploadController that uses a service UploadService to upload a file to an ftp server with org.apache.commons.net.ftp.FTPClient. I would like to be able to send information back to the user if the ftp client was unable to connect or timed out, or successfully sent the file. I have some IOException handling, but I don't know how to turn that around and send it back to the front-end. Any help appreciated, thanks!
public void upload(InputStream inputStream) {
String filename = "file.txt"
client = new FTPClient();
try {
client.connect("ftpsite");
client.login("username", "password");
client.storeFile(filename, inputStream);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (inputStream!= null) {
inputStream.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return null;
}
You should throw a new Exception in your catch statement.
For example, you could create a RequestTimeoutException class:
#ResponseStatus(HttpStatus.REQUEST_TIMEOUT)
public class RequestTimeoutException extends RuntimeException { }
and then throw it when need be:
catch (IOException ioe) {
//do some logging while you're at it
throw new RequestTimeoutException();
}