aspose file tools*
The moose likes JDBC and the fly likes PrintWriter doesnt want to write all Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "PrintWriter doesnt want to write all" Watch "PrintWriter doesnt want to write all" New topic
Author

PrintWriter doesnt want to write all

Fadai Ganjaliyev
Greenhorn

Joined: May 19, 2008
Posts: 22
Hello!
I'm writing a ResultSet data delmited by '*' into a text file
using the PrintWriter.
The ResultSet is ok, but when writing nearly 45.000 it's fine,
when two times larger it's all right, but when three times
seems PrintWriter doesnt want to go on writing for some reason
and stops without any exception.
Cannot really find out why.
Please suggest what could be the possible reasons?
Thanks!!!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8968
    
    9

Are you invoking close() on the writer when you are done with it?
Are you reporting all exceptions?
Can you show us a simple example of code that exhibits this behavior?


[How To Ask Questions On JavaRanch]
Fadai Ganjaliyev
Greenhorn

Joined: May 19, 2008
Posts: 22
Hi, yes the PrintWriter is closed at the end.
This is the sample code



No exception is thrown.
Writes about 45000 records, 90.000 as well, but more than 92.000 does not.
Thanks for the reply.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8968
    
    9

You should invoke close() and flush() (Writers automatically flush their underlying streams when closed) in a finally block. That way they get cleaned up if an exception occurs or not.
I notice that there is an object, selectStat, that appears to execute your queries and control the logic in this block. Wouldn't it be more likely that the problem lies in that class than in the well-tested Java API?
One last minor performance thing:

It's a good idea to split that call into two print statements, one for the variable and one for the literal. Otherwise you're creating a StringBuffer object to perform string concatenation with each iteration and it's just unnecessary churn.
Fadai Ganjaliyev
Greenhorn

Joined: May 19, 2008
Posts: 22
Thanks for suggestions.
selectStat is a simple SELECT that reads data from Oracle tables.
Placing close() and flush() in the finally block didnt help.
I removed PrintWriter and put System.out.println() instead, the result is
the same.
The matter is not in the server memory for sure, because in PL/SQL
developer this query executes good returning 1.5 million of rows.
Even with System.out.println() the query stops after the 92000th (plus mines 5 rows) row.
"Stop" in the direct sense: System.out.println() suspends (IntelliJ IDEA)
and no exception is thrown.
Thanks for the reply.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8968
    
    9

Originally posted by Fadai Ganjaliyev:
Placing close() and flush() in the finally block didnt help.


I wouldn't expect it would in your case. It's good programming form, not a logic error (and again, Writers flush automatically when closed so it's not necessary to call flush()).

Originally posted by Fadai Ganjaliyev:

"Stop" in the direct sense: System.out.println() suspends (IntelliJ IDEA) and no exception is thrown.


Have you tried running it outside of IntelliJ?
What does selectStat do that just plain JDBC classes don't do? Have you tried using plain JDBC to accomplish the same thing? Since I haven't seen that code and it's obviously integral to the logic it's my primary suspicion.
Fadai Ganjaliyev
Greenhorn

Joined: May 19, 2008
Posts: 22
Hi,
Running it outside IntelliJ was the first thing I did, which
was giving the same result.
As you could see from the code I'm using JDBC and nothing else.
Fadai Ganjaliyev
Greenhorn

Joined: May 19, 2008
Posts: 22
I could completely understand what you mean by this
"What does selectStat do that just plain JDBC classes don't do?"
PLease clarify.
Thanks!!!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8968
    
    9

This is basic debugging:
You are having a problem retrieving data and writing it to a file.
Since you aren't getting an exception and the Java SQL and IO classes are well-known, the problem probably isn't with them.
The code you've shown us so far is straightforward and I don't see any problems.
The process of elimination points us to selectStat as the problem. What does this class do? Try replacing it in your loop above with plain JDBC calls and see if the problem persists. Show us the selectStat code so we can see what's going on.
Fadai Ganjaliyev
Greenhorn

Joined: May 19, 2008
Posts: 22
Thanks for the reply.
Here what selectStat is
Fadai Ganjaliyev
Greenhorn

Joined: May 19, 2008
Posts: 22
Hi, strange might seem, this works



but this


on a ceratin step stops.
PLease suggest.
Fadai Ganjaliyev
Greenhorn

Joined: May 19, 2008
Posts: 22
Well, noticed another thing.
In the following loop this

woks fine, but this

hangs.


Turns out the reason is in retrieving data.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Does it also hang when using getString instead of getObject?

I also suggest a move to JDBC.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Fadai Ganjaliyev
Greenhorn

Joined: May 19, 2008
Posts: 22
Hello!
Well, with getString() hangs as well.
Could you please say what do you mean by "moving to JDBC"?
As far as I know the code I use to connect to the database
is a part of it, isnt it?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

The moving suggestion was actually for this forum's bartenders or a sheriff

I think this thread will be handled quicker if it is moved to the JDBC forum, since it is clearly a JDBC problem if it hangs on getting values from your result set. Until it is moved we can continue here though - no need for a new thread.
[ June 04, 2008: Message edited by: Rob Prime ]
Fadai Ganjaliyev
Greenhorn

Joined: May 19, 2008
Posts: 22
I'm an ordinary memeber and I cannot move it
"Only administrators or moderators may perform this action"
is displayed when trying to move.
Could you please move it?
Fadai Ganjaliyev
Greenhorn

Joined: May 19, 2008
Posts: 22
Hi, Rob!
I was wondering if there were any other suggestions?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

I'm sorry, but I don't know what the problem is. In this forum there will surely be someone else who can help you though.
Fadai Ganjaliyev
Greenhorn

Joined: May 19, 2008
Posts: 22
Thanks for all anyway. I also hope that someone else will suggest smth.
Fadai Ganjaliyev
Greenhorn

Joined: May 19, 2008
Posts: 22
Hi, Rob.
Finally got that
"Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space".

But yet dont know what is the general solution of it.
Fadai Ganjaliyev
Greenhorn

Joined: May 19, 2008
Posts: 22
Rob, request turns into this.
I know beforehand that the ResultSet stores nearly 120.000 rows.
How can I read part by part(say by 10.000) rows from this result set so that there is no need to increase the JVM Heap Size?
PLease suggest.
Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Please don't address me directly. That may scare off other people who were willing to respond. Also, I don't read this forum regularly so you may wait long times without a reply.

You can increase the memory Java is allowed to use by using the "-Xmx<amount>" flag when starting Java. For instance, "java -Xmx256m MyClass". For more information, perform a search on this forum - this problem has been addressed multiple times.

As for your limiting, it depends on the SQL type. In MySQL, you add a "LIMIT 10000" to the end of the query. In Microsoft SQL Server, you add a "TOP 10000" directly after the SELECT word. For others (like Oracle, PostGres, etc), please check the documentation of the SQL type.
Fadai Ganjaliyev
Greenhorn

Joined: May 19, 2008
Posts: 22
Hello all again.
Just wanted to remind that the problem was transferring data from Oracle to SQL Server.
After comming accross with the Heap Size of JVM I decided to make a procedure on the Oracle side which reads data from it's own tables and writes lines to a text file on the server. After I move the file onto the SQL server where I load data from it with the aid of BULK INSERT.
This method works with any number of rows( nearly 15 million in my case)
and there is no any load on the client running the soft.
Bye all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PrintWriter doesnt want to write all