• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

Fixed loadCharacterInternal

Junior Spellweaver
Joined
Sep 23, 2012
Messages
129
Reaction score
17
a small memory leak was found and here is a much better loading of the chars :)

found in MapleClient.Java

should works on any source if you know what to do and it uses the Diamond Operator in Java 7

Code:
 private List<CharNameAndId> loadCharactersInternal(int serverId) {
        List<CharNameAndId> chars = new LinkedList<>();
         try {
            try (PreparedStatement ps = DatabaseConnection.getConnection().prepareStatement("SELECT id, name FROM characters WHERE accountid = ? AND world = ?")) {
                ps.setInt(1, this.accId);
                ps.setInt(2, serverId);
                 try (ResultSet rs = ps.executeQuery()) {
                     while (rs.next()) {
                         chars.add(new CharNameAndId(rs.getString("name"), rs.getInt("id")));
                     }
                     rs.close();
                     ps.close();
                 }
            }
         } catch (SQLException Ex) {
             System.out.println("loadCharactersInternal Error " + Ex.getMessage());
         }
         return chars;
    }
 
Last edited:
Initiate Mage
Joined
Sep 8, 2009
Messages
2
Reaction score
7
If you're gonna do it with try-with-resources, then you might as well remove the .close() calls, since that's what try-with-resources is for anyways...

The outer try-catch block can be merged with the try-with-resources block right after it.

... and moreover, if you're gonna do try-with-resources on things, I imagine there are some hundred more SQL statements in any Odin source that would like some of that magic. Wanna go "fix" those too?
 
may web.very maple.pls.
Loyal Member
Joined
Aug 12, 2009
Messages
1,810
Reaction score
606
I found this bug awhile ago, but I never used 2 try on it though o.o..
 
Initiate Mage
Joined
Sep 8, 2009
Messages
2
Reaction score
7
There are two try-with-resources blocks because both PreparedStatement and ResultSet are nasty database objects which need to be closed safely.
 
Supreme Arcanarch
Joined
Apr 1, 2012
Messages
946
Reaction score
329
Oh, how did the bug effected me before I changed the function to this?
 
Initiate Mage
Joined
Sep 8, 2009
Messages
2
Reaction score
7
You didn't know what the code did and you integrated it anyways? ... why?

I can't tell you exactly what the effects are since I don't know what your code was before you changed it.

In general, try-with-resources "ensures" that the object you pass into it (in this case the PreparedStatement and ResultSet objects) are closed before you exit the block. People usually forget to do this, or do it just by calling ".close()" on it (which is bad because the code may throw before it reaches the call, leaving the objects as they are), but try-with-resources does this automatically at the end of the block (internally it does this by putting the close calls in a finally block), and does the exception checking for you too. All that is very ugly to write and read, so it's one of the niftier features of JDK7.

But, like I said, no clue without knowing what your source looked like before you changed it.
 
Junior Spellweaver
Joined
Sep 23, 2012
Messages
129
Reaction score
17
I will look into it later thanks softee there was a bug where it could not load chars after it was created even it was in the sql oh yeah and I will try to use finally block will try to improve the code when I have time..
 
Back
Top