Jump to content

Question

Posted

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;
        }

9 answers to this question

Recommended Posts

  • 0
Posted

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

  • 0
Posted
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;
+        }
+    }
+    

  • 0
Posted (edited)
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
  • 0
Posted
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;
+                }
+            }

  • 0
Posted

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

  • 0
Posted

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

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×
×
  • 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