Исключение в потоке "main" java .lang.RuntimeException: org. postgresql .util.PSQLException: FATAL: извините, слишком много клиентов уже - PullRequest
0 голосов
/ 27 февраля 2020

У меня есть это исключение в моем коде, поэтому:

Вот код, в котором у меня есть исключение:

private void assignCoursesToStudents() throws SQLException, ClassNotFoundException, IOException {

    ConnectionToDatabase connectionToDatabase = new ConnectionToDatabase();

Random random = new Random();

List<Student> students = generateStudents();
    for (int i = 0; i < students.size(); i++) {
    int randomNumberOfStudentsToOneCourse = random.nextInt(3) + 1;

    Set<Integer> randomInts = new HashSet();
    while (randomInts.size() < randomNumberOfStudentsToOneCourse) {
        randomInts.add(random.nextInt(10) + 1);
    }
    for (int j = 0; j < randomInts.size(); j++) {
        insertStudentsToCoursesToDatabase(i, j, randomInts,connectionToDatabase);
    }
}

}

метод insertStudentsToCoursesToDatabase:

 private void insertStudentsToCoursesToDatabase(int i, int j, Set<Integer> randomInts, ConnectionToDatabase connectionToDatabase) throws IOException, ClassNotFoundException, SQLException {



    try {
        try (PreparedStatement preparedStatement = connectionToDatabase.connectToDatabase()
                .prepareStatement("INSERT INTO students_courses (student_id, course_id) VALUES(?, ?)")) {
            preparedStatement.setInt(1, i + 1);
            preparedStatement.setInt(2, (int) randomInts.toArray()[j]);
            preparedStatement.executeUpdate();
        }
    } catch (SQLException ex) {
        throw new RuntimeException(ex);
// HERE I HAVE AN EXCEPTION!

    }
}

Также у меня есть класс, где я подключаюсь к базе данных:

public class ConnectionToDatabase {

public Connection connectToDatabase() throws IOException, ClassNotFoundException, SQLException {

    BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\db.properties"));
    Properties properties=new Properties ();
    properties.load (bufferedReader);
    String driverName= (String) properties.get ("jdbc.sql.driver");

    String url= (String) properties.get ("jdbc.sql.url");
    String username = (String) properties.get ("jdbc.sql.username");
    String password= (String) properties.get ("jdbc.sql.password");
    Class.forName(driverName);

    Connection connection = DriverManager.getConnection(
            url, username, password);


    return connection;
}

}

И мое исключение:

Exception in thread "main" java.lang.RuntimeException: org.postgresql.util.PSQLException: FATAL: sorry, too many clients already
at com.foxminded.sql.util.GeneratedData.insertStudentsToCoursesToDatabase(GeneratedData.java:58)
at com.foxminded.sql.util.GeneratedData.assignCoursesToStudents(GeneratedData.java:41)
at com.foxminded.sql.util.GeneratedData.generateTestData(GeneratedData.java:23)
at com.foxminded.sql.dao.SchoolDaoImplementation.initializeTheDatabase(SchoolDaoImplementation.java:360)
at com.foxminded.sql.Main.main(Main.java:16)

Когда я пытаясь запустить код без класса «ConnectionToDatabase» и установить соединение непосредственно в нужном мне классе, у меня не было никаких ошибок.

...