• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

SQLite problems in a Java Game Plugin - causing server crash  RSS feed

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings everyone!

Over the past few weeks I've been writing a plugin for a game. The developers have released a API with event calls that you can build on. My plugin was going fine and on my test server it works as expected. But, when I upload the plugin onto my server which hosts the game after 10-15 minutes I get a crash (due to I expect more people calling these events).

The first crash caused:



So I though it was because I had not wrapped my SQLite commands with try catch statements. So I did this, and after some time on the server, with more people joining and doing stuff (causing these events to be called) I got another crash but this time it was (with my newly wrapped SQL statements:





Here is an example of one of my event methods with the SQLite statement in:







Can anyone see what I'm doing wrong here?
 
Bartender
Posts: 19668
92
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's probably a bug in the JVM, not in your program. Try a different JVM version and see if the problem goes away.
 
Mark Oneill
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys! Thought I would share how I fixed this (in case anyone else stumbles upon this looking for an answer to their same problem).

It turns out that by using the same ResultSet object (in my case rs) for multiple queries caused these kind of errors.

Each query needs its OWN ResultSet object.

I now use use the approach 1st result set is rs, second is rs2, third rs3 etc.

Since doing this I've not had any problems!

 
Rancher
Posts: 3596
39
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could cut down the number of db accesses there.
Just get the PlayerUID for the given AreaID.
If it matches then exit, if not then do the Friends query.
That middle SELECT is a bit unnecessary.
Indeed, you might be able to come up with a single query, but not knowing the logic I can't say.

In addition I would recommend reading up on PreparedStatements.
Seeing concatenated queries like that makes me itchy.


Oh yes, and your ResultSet should be closed off in a finally block (or use a try-with-resources).
At the moment, if you get an exception inside there you may end up leaking resources as the ResultSet(s) won't be closed.
 
Mark Oneill
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:You could cut down the number of db accesses there.
Just get the PlayerUID for the given AreaID.
If it matches then exit, if not then do the Friends query.
That middle SELECT is a bit unnecessary.
Indeed, you might be able to come up with a single query, but not knowing the logic I can't say.

In addition I would recommend reading up on PreparedStatements.
Seeing concatenated queries like that makes me itchy.


Oh yes, and your ResultSet should be closed off in a finally block (or use a try-with-resources).
At the moment, if you get an exception inside there you may end up leaking resources as the ResultSet(s) won't be closed.





Thank you for your post Dave. This is my first "real world" attempt at writing any Java / SQL commands since my Uni days (almost 10 years ago now). Indeed I can now see that the middle query is completely redundant! And as there are 30 odd of these events listeners, and potentially dozens of people calling them on a regular basis the removal of that query alone will make a lot of difference! I thank you again for pointing this out! :-)

Also, I had not heard about Prepared Statements - looks really useful. I will en-devour to upgrade my code to use these! 





 
Mark Oneill
Greenhorn
Posts: 16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll also add a finally block!
 
Mark Oneill
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Am I right to believe that the rs.close() statement before the return statement is redundant? As the finally block is always executed and therefore rs.close() is called twice depending on the result set?
 
Dave Tolls
Rancher
Posts: 3596
39
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
As you say, the finally will always execute.
 
Sheriff
Posts: 23646
49
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And another by the way, when you handle an exception and you don't really know what to do so you just print it to the console, it's better to do this:



That method prints the whole stack trace, instead of just the description of the exception. It tells you where the exception occurred, which is much more helpful than just what the exception was.
 
Mark Oneill
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice Paul - I will amend my exceptions with that! :-)
 
Mark Oneill
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, I've tried adding the line e.printStackTrace(); into my exception handling, and the IDE (NetBeans 8.2) gives me the message "Throwable.printStackTrace() should be removed" ?
 
Marshal
Posts: 59765
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark Oneill wrote:. . . the IDE . . . gives me the message "Throwable.printStackTrace() should be removed" ?

Until you have sorted out the exception, disbelieve the IDE. IDEs arrogate the right to print that sort of warning and you retain the right to ignore it.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!