Jump to content
  • 0

Conection aCis


l2jkain

Question

could this remove? I'm adapting a frozen code for now and would like to know if removing this would give some error

 

finally
        {
            try
            {
                con.close();
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
            con = null;
        }

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

connections must be closed . otherwise Ressource leak will take place and MySQLNonTransientConnectionException  error will appear because of unclosed ressource

Link to comment
Share on other sites

  • 0
1 hour ago, SweeTs said:

Ye, remove that and use connection inside the loop. Look sources, tons of examples. 

 

but the loop would send to the gs, right? in case if any player change password will go to the gs by what I saw in that code there the exit is null

 

     
+    private void updPassKey(L2PcInstance player, String pass)
+    {
+        Connection con = null;
+        String query = "UPDATE passkey SET passkey = ? WHERE obj_Id = ?";
+        try
+        {
+            con = L2DatabaseFactory.getInstance().getConnection();
+            PreparedStatement st = con.prepareStatement(query);
+            st.setString(1, encodePass(pass));
+            st.setInt(2, player.getObjectId());
+            st.executeUpdate();
+            st.close();
+            st = null;
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+        finally
+        {
+            try
+            {
+                con.close();
+            }
+            catch (SQLException e)
+            {
+                e.printStackTrace();
+            }
+            con = null;
+        }
+    }
+    

Link to comment
Share on other sites

  • 0
try (Connection con = L2DatabaseFactory.getInstance().getConnection())
{
	PreparedStatement st = con.prepareStatement("UPDATE passkey SET passkey = ? WHERE obj_Id = ?");
	st.setString(1, encodePass(pass));
    st.setInt(2, player.getObjectId());
    st.executeUpdate();
    st.close();
}
catch (Exception e)
{
	 e.printStackTrace();
}

 

Simply use try-with-ressources statements, then java automatically will close the connection

 

Edit: Your error has nothing to do with the connection btw

Edited by melron
Link to comment
Share on other sites

  • 0
30 minutes ago, melron said:

try (Connection con = L2DatabaseFactory.getInstance().getConnection())
{
	PreparedStatement st = con.prepareStatement("UPDATE passkey SET passkey = ? WHERE obj_Id = ?");
	st.setString(1, encodePass(pass));
    st.setInt(2, player.getObjectId());
    st.executeUpdate();
    st.close();
}
catch (Exception e)
{
	 e.printStackTrace();
}

 

Simply use try-with-ressources statements, then java automatically will close the connection

 

Edit: Your error has nothing to do with the connection btw

 

the error is here because when I change the password of this error

+            else if (_command.equalsIgnoreCase("pkrecovery"))
+           {
+                String htmContent = HtmCache.getInstance().getHtm("data/html/passkey/recovery.htm");
+                NpcHtmlMessage html = new NpcHtmlMessage(1);
+                html.setHtml(htmContent);
+                html.replace("%question%", getPassKeyQuestion(activeChar));
+                activeChar.sendPacket(html);
+                html = null;
+           }
+            else if (_command.startsWith("pkset"))
+            {
+                StringTokenizer st = new StringTokenizer(_command, "]");
+            if (st.countTokens() != 5)
+                {
+                    activeChar.sendMessage("You have not entered all the data!");
+                    String htmContent = HtmCache.getInstance().getHtm("data/html/passkey/setup.htm");
+                    NpcHtmlMessage html = new NpcHtmlMessage(1);
+                    html.setHtml(htmContent);
+                    activeChar.sendPacket(html);
+                    html = null;
+                    return;
+                }
+                String newCommand = st.nextToken();
+                String pass1 = st.nextToken();
+                pass1 = pass1.substring(1, pass1.length() - 1);
+                String pass2 = st.nextToken();
+                pass2 = pass2.substring(1, pass2.length() - 1);
+                String question = st.nextToken();
+                question = question.substring(1, question.length() - 1);
+                String answer = st.nextToken();
+                answer = answer.substring(1, answer.length());
+                if (pass1 == null || pass2 == null || question == null || answer == null)
+                {
+                    activeChar.sendMessage("Input error");
+                    String htmContent = HtmCache.getInstance().getHtm("data/html/passkey/setup.htm");
+                    NpcHtmlMessage html = new NpcHtmlMessage(1);
+                    html.setHtml(htmContent);
+                    activeChar.sendPacket(html);
+                    html = null;
+                    return;
+                }
+                
+                if (!pass1.equals(pass2))
+                {
+                    activeChar.sendMessage("You entered different passwords");
+                    activeChar.sendMessage("pass1 = " + pass1);
+                    activeChar.sendMessage("pass2 = " + pass2);
+                    activeChar.sendMessage("Question = " + question);
+                    activeChar.sendMessage("answer = " + answer);
+                    String htmContent = HtmCache.getInstance().getHtm("data/html/passkey/setup.htm");
+                    NpcHtmlMessage html = new NpcHtmlMessage(1);
+                    html.setHtml(htmContent);
+                    activeChar.sendPacket(html);
+                    html = null;
+                    return;
+                }
+                insertPassKeyInformation(activeChar, pass1, question, answer);
+                String htmContent = HtmCache.getInstance().getHtm("data/html/passkey/login.htm");
+                NpcHtmlMessage html = new NpcHtmlMessage(1);
+                html.setHtml(htmContent);
+                activeChar.sendPacket(html);
+                html = null;
+            }
+            else if (_command.startsWith("pklogin"))
+            {
+                StringTokenizer st = new StringTokenizer(_command, " ");
+                if (st.countTokens() != 2)
+                {
+                    activeChar.sendMessage("You make a mistake when entering the password!");
+                    String htmContent = HtmCache.getInstance().getHtm("data/html/passkey/login.htm");
+                    NpcHtmlMessage html = new NpcHtmlMessage(1);
+                    html.setHtml(htmContent);
+                    activeChar.sendPacket(html);
+                    html = null;
+                    return;
+                }
+                String newCommand = st.nextToken();
+                String pass = st.nextToken();
+                Connection con = null;
+                String query = "SELECT passkey FROM passkey WHERE obj_Id = ?";
+                String pwdindb = "error";
+                try
+                {
+                con = L2DatabaseFactory.getInstance().getConnection();
+                PreparedStatement ps = con.prepareStatement(query);
+                ps.setInt(1, activeChar.getObjectId());
+                ResultSet rs = ps.executeQuery();
+                    
+                while (rs.next())
+                    pwdindb = rs.getString(1);
+                    rs.close();
+                    ps.close();
+                    ps = null;
+                    rs = null;
+                }
+                catch (Exception e)
+                {
+                e.printStackTrace();
+                }
+                finally
+                {
+                    try
+                    {
+                        con.close();
+                    }
+                    catch (SQLException e)
+                    {
+                        e.printStackTrace();
+                    }
+                    con = null;
+                }
+                if (pwdindb.equals(encodePass(pass)))
+                {
+                    activeChar.setIsParalyzed(false);
+                    activeChar.setIsSubmitingPin(false);
+                    activeChar.setIsInvul(false);
+                }
+                else
+                {
+                    activeChar.sendMessage("You have not entered the correct password");
+                    String htmContent = HtmCache.getInstance().getHtm("data/html/passkey/login.htm");
+                    NpcHtmlMessage html = new NpcHtmlMessage(1);
+                    html.setHtml(htmContent);
+                    activeChar.sendPacket(html);
+                    html = null;
+                    return;
+                }
+            }
+            else if (_command.startsWith("pkrec"))
+            {
+                StringTokenizer st = new StringTokenizer(_command, " ");
+                if (st.countTokens() != 4)
+                {
+                    activeChar.sendMessage("You make a mistake when entering data!");
+                    String htmContent = HtmCache.getInstance().getHtm("data/html/passkey/recovery.htm");
+                    NpcHtmlMessage html = new NpcHtmlMessage(1);
+                    html.setHtml(htmContent);
+                    html.replace("%question%", getPassKeyQuestion(activeChar));
+                    activeChar.sendPacket(html);
+                    html = null;
+                    return;
+                }
+                String newCommand = st.nextToken();
+                String answer = st.nextToken();
+                String pass1 = st.nextToken();
+                String pass2 = st.nextToken();
+                if (!pass1.equals(pass2))
+                {
+                    activeChar.sendMessage("You entered different passwords");
+                    String htmContent = HtmCache.getInstance().getHtm("data/html/passkey/recovery.htm");
+                    NpcHtmlMessage html = new NpcHtmlMessage(1);
+                    html.setHtml(htmContent);
+                    html.replace("%question%", getPassKeyQuestion(activeChar));
+                    activeChar.sendPacket(html);
+                    html = null;
+                    return;
+                }
+                Connection con = null;
+                String query = "SELECT answer FROM passkey WHERE obj_Id = ?";
+                  String anwindb = "error";
+                try
+                {
+                    con = L2DatabaseFactory.getInstance().getConnection();
+                    PreparedStatement ps = con.prepareStatement(query);
+                    ps.setInt(1, activeChar.getObjectId());
+                    ResultSet rs = ps.executeQuery();
+                    
+                    while (rs.next())
+                        anwindb = rs.getString(1);
+                    
+                    rs.close();
+                    ps.close();
+                    ps = null;
+                    rs = null;
+                }
+                catch (Exception e)
+                {
+                    e.printStackTrace();
+                }
+                finally
+                {
+                    try
+                    {
+                        con.close();
+                    }
+                    catch (SQLException e)
+                    {
+                        e.printStackTrace();
+                    }
+                    con = null;
+                }
+                
+                if (anwindb.equals(answer))
+                {
+                    updPassKey(activeChar, pass1);
+                    activeChar.sendMessage("You have successfully changed your password.");
+                    String htmContent = HtmCache.getInstance().getHtm("data/html/passkey/login.htm");
+                    NpcHtmlMessage html = new NpcHtmlMessage(1);
+                    html.setHtml(htmContent);
+                    activeChar.sendPacket(html);
+                    html = null;
+                }
+                else
+                {
+                    activeChar.sendMessage("You entered the wrong answer to your question");
+                    String htmContent = HtmCache.getInstance().getHtm("data/html/passkey/recovery.htm");
+                    NpcHtmlMessage html = new NpcHtmlMessage(1);
+                    html.setHtml(htmContent);
+                    html.replace("%question%", getPassKeyQuestion(activeChar));
+                    activeChar.sendPacket(html);
+                    html = null;
+                    return;
+                }
+            }

Link to comment
Share on other sites

  • 0

Its obvious that is there (RequestBypassToServer) but not in mysql connection. Debug your code and find your line

Link to comment
Share on other sites

  • 0

Many thanks to both of you I have re-created the code and removed the garbage and updated the closing method you mentioned

2 hours ago, melron said:

Its obvious that is there (RequestBypassToServer) but not in mysql connection. Debug your code and find your line

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

AdBlock Extension Detected!

Our website is made possible by displaying online advertisements to our members.

Please disable AdBlock browser extension first, to be able to use our community.

I've Disabled AdBlock