I created an application which stores values into the database and retrieves the stored data. While running an application in run mode everything seems to work fine (the values are stored and retrieved successfully) but when I run in the debug mode the process throws IllegalStateException and so far haven't found a cause.
The method which retrieves an object Recording is the following:
public Recording getRecording(String filename) {
Recording recording = null;
String where = RECORDING_FILENAME + "='" + filename + "'";
Log.v(TAG, "retrieving recording: filename = " + filename);
try {
cursor = db.query(DATABASE_TABLE_RECORDINGS, new String[]{RECORDING_FILENAME, RECORDING_TITLE, RECORDING_TAGS, RECORDING_PRIVACY_LEVEL, RECORDING_LOCATION, RECORDING_GEO_TAGS, RECORDING_GEO_TAGGING_ENABLED, RECORDING_TIME_SECONDS, RECORDING_SELECTED_COMMUNITY}, where, null, null, null, null);
if (cursor.getCount() > 0) {
cursor.moveToFirst();
//String filename = c.getString(0);
String title = cursor.getString(1);
String tags = cursor.getString(2);
int privacyLevel = cursor.getInt(3);
String location = cursor.getString(4);
String geoTags = cursor.getString(5);
int iGeoTaggingEnabled = cursor.getInt(6);
String recordingTime = cursor.getString(7);
String communityID = cursor.getString(8);
cursor.close();
recording = new Recording(filename, title, tags, privacyLevel, location, geoTags, iGeoTaggingEnabled, recordingTime, communityID);
}
}
catch (SQLException e) {
String msg = e.getMessage();
Log.w(TAG, msg);
recording = null;
}
return recording;
}
and it is called from another class (Settings):
private Recording getRecording(String filename) {
dbAdapter = dbAdapter.open();
Recording recording = dbAdapter.getRecording(filename);
dbAdapter.close();
return recording;
}
While running through the code above everything works fine but then I notice an exception in another thread:
alt text http://img509.imageshack.us/img509/862/illegalstateexception.jpg
and don't know neither what is the possible cause of this exception nor how to debug the code from that thread to diagnose the cause.
I would be very thankful if anyone knew what is the possible issue here.
Thank you!
Looks like that cursor.close() is inside an "if" - that's when SQLiteCursor.finalize() will throw an IllegalStateException (I googled for it). You migh be getting an empty recordset, for instance, if some other process/thread didn't have the time to commit.
Close it always, even if it's result set is empty.
I'd also advice you to access fields by names, not indices, for future compatibility. And do both close()s in finally{} blocks.
Related
I've got an SSIS package that is responsible for picking up and loading files to SQL tables, and then routing the files to various archive locations depending upon the content of the file. Prior to doing any loading, however, I have some scripts that check for various file-level errors, one of which is if the file is empty or not. I am using the following script to check if a file is empty:
Dts.Variables["blnEmptyFile"].Value = false;
Dts.Variables["User::strFileTimeStamp"].Value = "_" + DateTime.Now.ToString("yyyyMMdd") + "_" + DateTime.Now.ToString("hhmmss");
if (Dts.Variables["User::strFileRecordType"].Value.ToString() == "AF")
{
Dts.Variables["User::strFileOutToGeBBs"].Value = "AF_" + Dts.Variables["User::strCurrentFileIn"].Value.ToString().Substring(0, Dts.Variables["User::strCurrentFileIn"].Value.ToString().Length - 7) + Dts.Variables["User::strFileTimeStamp"].Value.ToString() + ".txt";
}
else if (Dts.Variables["User::strFileRecordType"].Value.ToString() == "AS")
{
Dts.Variables["User::strFileOutToGeBBs"].Value = "AS_" + Dts.Variables["User::strCurrentFileIn"].Value.ToString().Substring(0, Dts.Variables["User::strCurrentFileIn"].Value.ToString().Length - 7) + Dts.Variables["User::strFileTimeStamp"].Value.ToString() + ".txt";
}
else if (Dts.Variables["User::strFileRecordType"].Value.ToString() == "R")
{
Dts.Variables["User::strFileOutToGeBBs"].Value = "R_" + Dts.Variables["User::strCurrentFileIn"].Value.ToString().Substring(0, Dts.Variables["User::strCurrentFileIn"].Value.ToString().Length - 6) + Dts.Variables["User::strFileTimeStamp"].Value.ToString() + ".txt";
}
else
{
Dts.Variables["User::strFileOutToGeBBs"].Value = Dts.Variables["User::strCurrentFileIn"].Value.ToString().Substring(0, Dts.Variables["User::strCurrentFileIn"].Value.ToString().Length - 4) + Dts.Variables["User::strFileTimeStamp"].Value.ToString() + ".txt";
}
int nonDataRows = 1;
string ffConnection;
ffConnection = (string)(Dts.Connections["CDF_Format"].AcquireConnection(null) as string);
FileInfo flatFileInfo = new FileInfo(ffConnection);
long fileSize = flatFileInfo.Length;
if (fileSize > 0)
{
int lineCount = 0;
StreamReader fsFlatFile = new StreamReader(ffConnection);
while (!(fsFlatFile.EndOfStream))
{
Console.WriteLine(fsFlatFile.ReadLine());
lineCount += 1;
if (lineCount > nonDataRows)
{
Dts.Variables["User::blnEmptyFile"].Value = false;
break;
}
else
{
Dts.Variables["User::blnEmptyFile"].Value = true;
}
}
fsFlatFile.Close();
fsFlatFile = null;
}
else
{
Dts.Variables["User::blnEmptyFile"].Value = true;
}
Dts.TaskResult = (int)ScriptResults.Success;
After this script task, I check the blnEmptyFile variable, and if it is True, I send the empty file to a failed files folder location so that our end users can deal with it. And when I run this script inside the editor (VS 2017/2019), it runs to successful completion every single time. However, when this is run on the ETL servers, it intermittently fails with the following error:
2022-01-18 07:15:14.27 fst_ArchFailedFile:Error: An error occurred with the following error message: "The process cannot access the file because it is being used by another process.".
This only ever happens when a file is empty, and even then, it is only happening with a specific set of files that are empty. And it doesn't happen all the time; it is intermittent, and seems to want to happen on Monday (or, in the case of long weekends, Tuesday) mornings.
As you can see in the code, I am closing fsFlatFile, and I'm setting it to null. ffConnection is a string, so I can't close/kill it...although setting it to NULL has no effect either. I've tried setting a PAUSE, and that also has no effect/bearing on what is happening.
Anybody have any ideas why this would happen, and how I can resolve this? We aren't talking about Excel here (I've seen all of those posts on the internet), but simple TXT files that have only but a header and nothing else in them. Any help here would be keen.
StreamReader still has it. Try using a using block, everything inside is properly closed, disposed, etc:
using(StreamReader fsFlatFile = new StreamReader(ffConnection))
{
All the code you want to do while your stream is open.
}
I send a document over socket like this:
sendFXML(asByteArray(getRequiredScene(fetchSceneRequest())));
private void sendFXML(byte[] requiredFXML) throws IOException, TransformerException {
dataOutputStream.write(requiredFXML);
dataOutputStream.flush();
}
private Document getRequiredScene(String requiredFile) throws IOException, ParserConfigurationException, SAXException, TransformerException {
return new XMLLocator().getDocumentOrReturnNull(requiredFile);
}
private String fetchSceneRequest() throws IOException, ClassNotFoundException {
return dataInputStream.readUTF();
}
On the side of XMLLocator it finds the correct document and parses it right. I see it by printing the whole doc in console.
But I cannot handle it on the clients side where it's fetch by:
public static void receivePage() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] data = new byte[989898];
int bytesRead = -1;
while((bytesRead = dataInputStream.read(data)) != -1 ) { //stops here
baos.write(data, 0, bytesRead );
}
Files.write(Paths.get(FILE_TO_RECEIVED), data);
}
After the first iteration in while() cycle it just stops on the commented place.
I don't know if I have an error on the side of the server and I send this in doc in an incorrect format or I read the sent byte array incorrectly. Where is the problem?
Edit:
For the debug purpose, in the receivePage() method, I've chosen a different way of reading the byte array from server which goes like:
int count = inputStream.available();
byte[] b = new byte[count];
int bytes = dataInputStream.read(b);
System.out.println(bytes);
for (byte by : b) {
System.out.print((char)by);
}
And now I'm able to print fetched FXLM in console but a new problem has appeared.
On debug, it normally receives the byte[] from server, writes 2024 for count and displayes the content of the file but if I run the app normally via Shift + f10 it fetches nothing and just writes 0 in console
Edit2:
For some reason, once again, on debug, it's able to even write into a file
for (byte by : b) {
Files.write(Paths.get(FILE_TO_RECEIVED), b);
System.out.print((char)by);
}
But when I try to return this fxml on debug and then show like this:
Parent fxmlToShow = FXMLLoader.load(getClass().getResource("/network/gui.fxml"));
Scene childScene = new Scene(fxmlToShow);
Stage window = (Stage)((Node)ae.getSource()).getScene().getWindow();
window.setScene(childScene);
return window;
It shows only previous files. Like on the first attempt of debug it show a blank page when I asked for the 1st one from server. On the second attempt of debug when i ask for 3rd page from server, it shows me the previously asked one and so on.
To me, it seems absolutely insane cuz the fxml rile actually refreshes before the line
Parent fxmlToShow = FXMLLoader.load(getClass().getResource("/network/gui.fxml"));
is invoked.
Yeah, thank everybody for participating.
So, the issue of incorrect displaying if FXML files was caused by the incorrect FILE_TO_RECEIVED path.
When FXMLLoader.load(getClass().getResource("/network/gui.fxml")); loads gui.fxml it takes it not from D:\\JetBrains\\IdeaProjects\\Client\\src\\network\\gui.fxml,im my case, but from D:\\JetBrains\\IdeaProjects\\Client\\OUT\\PRODUCTION\\Client\\network\\gui.fxml.
As for me, that doesn't seem obvious.
What about different behaviour on debug and on run. In method receivePage() it needs to wait until connection is available.
int count = inputStream.available();
If you read docs for this method you will see
Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream ...
The available method for class InputStream always returns 0...
So, you jext need to wait for connection to be available
while(inputStream.available()==0){
Thread.sleep(100);
}
Otherwise it just prepares byte[] b = new byte[count]; for 0 bytes and you can write in nothing.
I'm new to programming and working on this project of Library System.
It uses linked lists to save details of books and members.
It has two functions that print details of all books in library and all members of library.
As I made a GUI for my project I can't print details in Frame.
So, I thought of doing it with file.
I have successfully printed everything in file but now my problem is how should I display file to user?
Like if user presses "show all books" button, it should automatically open the respective file.
I tried searching but I can't figure out what to actually search as I'm a beginner.
Any help would be much appreciated please.
UPDATE:
I tried with Desktop.getDesktop(file) and I am getting this error.
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "Book issued successfully"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.parseLong(Long.java:631)
at RunProgram.stringToLong(RunProgram.java:217)
at RunProgram.actionPerformed(RunProgram.java:196)
This is my stringToLong method
private static long stringToLong(String stringObject){
return Long.parseLong(stringObject.trim());
}
This is the method for printing details
void printBooksIssued(long cpr) throws IOException{
File file = new File("d:/work/test.txt");
System.setOut(new PrintStream(new FileOutputStream(file)));
int index = 0;
if (searchMember(cpr) == -1)
System.out.println("Member doesn't exist.");
LibMember m = membersList.get(index);
while (index < sizeMembersList() ){
if (m.getCprNum() == cpr){
System.out.println(Arrays.toString(m.getBooksIssued()));
return;
}
index++;
if (index < sizeMembersList())
m = membersList.get(index);
}
Desktop.getDesktop().open(file);
}
And this is how I called this method inside actionListener method.
case "Print details of books issued to member": {
long cprNum = stringToLong(cpr.getText());
try {
ITLib.printBooksIssued(cprNum);
}
catch (IOException e1){
fName.setText("Error. Make sure CPR number is correct and try again.");
}
}
The thing I don't understand is that the first error of numberFormatException of string "Book issued successfully is completely separate from this button and textfield. Then why is it giving error with that.
case "Issue": {
long an = stringToLong(NUM.getText());
long cpr = stringToLong(CPR.getText());
if (ITLib.issueBook(an, cpr))
NUM.setText("Book issued successfully");
else
NUM.setText("Book couldn't be issued. Try again later.");
break;
}
If you are using Java and save the files with the suffix ".txt", you can use Desktop.getDesktop().
Here is an example that will popup the default system editor for text:
File file = new File("d:/work/test.txt");
System.setOut(new PrintStream(new FileOutputStream(file)));
System.out.println("Testing 1");
System.out.println("Testing 2");
Desktop.getDesktop().open(file);
On Windows this will open notepad.exe with the "text.txt" file.
When trying to run the code above I'm getting javax.naming.OperationNotSupportedException with the message:
[LDAP: error code 12 - 00000057: LdapErr: DSID-0C09079A, comment: Error processing control, data 0, v2580].
The first page is successfully retrieved and the exception is thrown only at second loop iteration.
public void pagedResults() {
PagedResultsCookie cookie = null;
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
int page = 1;
do {
logger.info("Starting Page: " + page);
PagedResultsDirContextProcessor processor = new PagedResultsDirContextProcessor(20, cookie);
List<String> lastNames = ldapTemplate.search("", initialFilter.encode(), searchControls, UserMapper.USER_MAPPER_VNT, processor);
for (String l : lastNames) {
logger.info(l);
}
cookie = processor.getCookie();
page = page + 1;
} while (null != cookie.getCookie());
}
However, when I remove Spring LDAP using pure implementation as above, it works!
try {
LdapContext ctx = new InitialLdapContext(env, null);
// Activate paged results
int pageSize = 5;
byte[] cookie = null;
ctx.setRequestControls(new Control[] { new PagedResultsControl(pageSize, Control.CRITICAL) });
int total;
do {
/* perform the search */
NamingEnumeration results = ctx .search("",
"(&(objectCategory=person)(objectClass=user)(SAMAccountName=vnt*))",
searchCtls);
/* for each entry print out name + all attrs and values */
while (results != null && results.hasMore()) {
SearchResult entry = (SearchResult) results.next();
System.out.println(entry.getName());
}
// Examine the paged results control response
Control[] controls = ctx.getResponseControls();
if (controls != null) {
for (int i = 0; i < controls.length; i++) {
if (controls[i] instanceof PagedResultsResponseControl) {
PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i];
total = prrc.getResultSize();
if (total != 0) {
System.out.println("***************** END-OF-PAGE "
+ "(total : " + total
+ ") *****************\n");
} else {
System.out.println("***************** END-OF-PAGE "
+ "(total: unknown) ***************\n");
}
cookie = prrc.getCookie();
}
}
} else {
System.out.println("No controls were sent from the server");
}
// Re-activate paged results
ctx.setRequestControls(new Control[] { new PagedResultsControl(
pageSize, cookie, Control.CRITICAL) });
} while (cookie != null);
ctx.close();
} catch (NamingException e) {
System.err.println("PagedSearch failed.");
e.printStackTrace();
} catch (IOException ie) {
System.err.println("PagedSearch failed.");
ie.printStackTrace();
}
Any hints?
The bad thing about LDAP paged results is that they only work if the same underlying connection is used for all requests. The internals of Spring LDAP get a new connection for each LdapTemplate operation, unless you use the transactional support.
The easiest way to make sure the same connection will be used for a sequence of LDapTemplate operations is to use the transaction support, i.e. configure transactions for Spring LDAP and wrap the target method with a Transactional annotation.
I managed to make my example above work using SingleContextSource.doWithSingleContext approach.
However my scenario is different, my app is service oriented and the paged results as well as the cookie should be sent to an external client so that he decides to request next pages or not.
So as far as I can tell, spring-ldap does not support such case. I must use pure implementation so that I can keep track of the underlying connection during requests. Transaction support could help as well as SingleContextSource, but not among different requests.
#marthursson
Is there any plan in spring ldap to such support in the future?
I found I could use your first example (Spring) as long as I set the ignorePartialResultException property to true in my ldapTemplate configuration and put the #Transactional on my method as suggested.
you can replace ldapTemplate DirContext like this
ldapTemplate.setContextSource(new SingleContextSource(ldapContextSource().getReadWriteContext()));
In my app I tried to pass the file path from one activity to another activity using intent.In my receiving activity I got the file path as "null".But when I print the file in first activity it prints the path.From my second activity I attach that file to mail using Gmailsender.This was the code I tried,
private void startRecord()
{
File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");
try
{
file.createNewFile();
OutputStream outputStream = new FileOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
DataOutputStream dataOutputStream = new DataOutputStream(bufferedOutputStream);
int minBufferSize = AudioRecord.getMinBufferSize(8000,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
short[] audioData = new short[minBufferSize];
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
8000,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
minBufferSize);
audioRecord.startRecording();
while(recording)
{
int numberOfShort = audioRecord.read(audioData, 0, minBufferSize);
for(int i = 0; i < numberOfShort; i++)
{
dataOutputStream.writeShort(audioData[i]);
}
}
audioRecord.stop();
audioRecord.release();
dataOutputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
String audiofile;
audiofile=file.getAbsolutePath();
System.out.println("File Path::::"+audiofile);
}
Intent is,
Intent sigout=new Intent(getApplicationContext(),WeeklyendActivity.class);
sigout.putExtra("mnt/sdcard-test.pcm",audiofile);
startActivity(sigout);
In my receiving activity,
String patty=getIntent().getStringExtra("mnt/sdcard-text.pcm");
System.out.println("paathhhy frfom ::"+patty);
It prints null.Can anyone help me how to get the file path.And more thing I am not sure whether the audio would save in that file correctly?
Please anyone help me!!!Thanks in advance!
Based on your information that audioFile is a variable of type File, when you do this:
sigout.putExtra("mnt/sdcard-test.pcm",audiofile);
you are putting a File object in the extras Bundle. Then, when you try to get the extra from the Bundle you do this:
String patty=getIntent().getStringExtra("mnt/sdcard-text.pcm");
However, the object in this extra is of type File, not type String. This is why you are getting null.
If you only want to pass the name of the file, then put the extra like this:
sigout.putExtra("mnt/sdcard-test.pcm",audiofile.getAbsolutePath());