0

enter image description here

I was working on a java project and it was working just fine. I was able to make connections. I closed all the connections properly in finally block. Now I am not able to make connections or even open psql in my terminal. How can I make it work as before. Much much appreciated

import java.sql.Connection;
import com.mchange.v2.c3p0.*;

public class MyConnection {
    public static Connection getConnection(){
        ComboPooledDataSource cpds1 = new ComboPooledDataSource(); 
        String dbDriver = "org.postgresql.Driver";
        String dbName = "jdbc:postgresql://localhost/postgres";
        cpds1.setJdbcUrl(dbName);
        String userName = "user_1";
        cpds1.setUser(userName);
        String password = "mypass";
        cpds1.setPassword(password);
        cpds1.setMaxStatements( 180 );  
        try 
        {
            cpds1.setDriverClass(dbDriver);
            return cpds1.getConnection();
        } 
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }
}

This is where I'm calling it

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        PrintWriter writer = response.getWriter();
        JSONObject jo = new JSONObject();
        JSONObject jObj;
        Statement stmt = null;
        Connection con = null;
        PreparedStatement ps;
        ResultSet rs = null;
        try
        {
            jObj = UtilityClass.getJSON(request);
            String uname = ((String) jObj.get("uname"));
            String pass = ((String) jObj.get("pass"));
            String sql = "SELECT * FROM users WHERE username = ?";
            try
            {
                con = MyConnection.getConnection();
                System.out.println("Got Connection");      
                stmt = con.createStatement();
                ps = con.prepareStatement(sql);
                ps.setString(1, uname);
                rs = ps.executeQuery();
                if(rs.next())
                {
                    if(BCrypt.checkpw(pass,rs.getString("password")))
                    {
                        HttpSession session = request.getSession();
                        session.setAttribute("uname", uname);
                        if(session.isNew())
                        {
                            System.out.println("new");
                        }
                        if(uname.equals("admin"))
                        {
                            session.setAttribute("role", "admin");
                            jo.put("status", "admin");
                        }
                        else
                        {
                            session.setAttribute("role", "user");
                            jo.put("status", "authenticate");
                        }
                    }
                }
                writer.print(jo);
            }
            catch (Exception e) 
            {
                e.printStackTrace();
                System.out.println("Not Connected");
            }
            finally 
            {
                if(rs != null)
                {
                    rs.close();
                }
                if(stmt != null)
                {
                    stmt.close();
                }
                if(con != null)
                {
                    con.close();
                }
            }
        }
        catch(Exception e)
        {
            System.out.print("JSON Exception");
        }
    }
Shelton
  • 47
  • 5
  • take a look here https://stackoverflow.com/questions/2757549/org-postgresql-util-psqlexception-fatal-sorry-too-many-clients-already. In any case you haven't show us your java code so can'T do much for you – J Asgarov Feb 09 '22 at 10:21
  • @JAsgarov I've edited the question. Please verify – Shelton Feb 09 '22 at 10:25
  • 3
    You're creating a new pool every time you request a connection. – tgdavies Feb 09 '22 at 10:32
  • @tgdavies I'm kinda new in this. What should I change here? And how can I free all the connections. Is there any way to do that? – Shelton Feb 09 '22 at 10:36
  • 2
    Create a static field in MyConnection to hold a ComboPooledDataSource instance, and set it up in a static initialiser. See https://www.mchange.com/projects/c3p0/#using_combopooleddatasource for more information about the class. – tgdavies Feb 09 '22 at 10:38
  • 2
    To free the connections restart your java process – tgdavies Feb 09 '22 at 10:39
  • @tgdavies restarting the java process? Would you mind to elaborate – Shelton Feb 09 '22 at 10:48
  • 1
    Stop your server process (the servlet engine) and start it again. – tgdavies Feb 09 '22 at 10:53
  • @tgdavies I did. But it still says "too many clients already in postgres" – Shelton Feb 09 '22 at 10:53
  • 1
    I would have expected that to fix it. You can also restart Postgres – tgdavies Feb 09 '22 at 10:55
  • @tgdavies Thanks a lot!! It worked.. You have no idea how happy I am TT – Shelton Feb 09 '22 at 10:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/241861/discussion-between-shelton-and-tgdavies). – Shelton Feb 09 '22 at 11:03
  • @tgdavies I've added my modified code in the chat. Can you please verify if that's proper? – Shelton Feb 09 '22 at 11:20

1 Answers1

0

Usually, DB Admins are using pooling technologies on Databases. For PostgreSQL one of the more popularly is a PGBOUNCER. We used PGBOUNCER in our large project, the result is excellent. I recommend it to you. To get more information about the pooling system you can read this link. For About Pooling

Ramin Faracov
  • 3,032
  • 1
  • 2
  • 8