简体   繁体   中英

Unstable Oracle Database connection for Java-project

I'm a student and one of our assignments is creating a Java web project on a local GlassFish 5 webserver. The database used for this project is an OracleDB running locally in a Docker container.

I almost finished my project but some pages keep crashing (NullPointerException). I have to retrieve database records and save them in an ArrayList. But sometimes the SQLConnection doesn't return anything (but the records DO exist) and my code tries to preform actions on that empty ArrayList.

Now, as I said, the connection appears to be unstable, because at some seemingly random moments the database does respond with the appropriate records.

It's really frustrating and I cannot continue working on this project without a stable database connection. So I'd appreciate hearing from people with some more experience :-)

Thank you for your time.

Code for running a query:

protected ResultSet getRecords(String query) {
        try {
            Connection connection = DriverManager.getConnection(url, login, password);
            Statement statement = connection.createStatement();
            return (ResultSet) statement.executeQuery(query);
        } catch (SQLException e) {
            e.getStackTrace();
        } 
        return null;
    }

Code with the query:

List<Uitlening> uitleningen = new ArrayList<Uitlening>();

        try {
            ResultSet resultSet = getRecords("SELECT * FROM uitlening");

            while(resultSet.next()) { //Here the code crashes because the ResultSet can sometimes be empty.

I think this is the actual error message: Listener refused the connection with the following error: ORA-12519, TNS:no appropriate service handler found

But I don't really understand what I should do now..

 try {
        ResultSet resultSet = getRecords("SELECT * FROM uitlening");

        while(resultSet.next()) { 
            Uitlening uitlening = new Uitlening();
            uitlening.setNr(resultSet.getInt("nr"));                
            uitleningen.add(uitlening);
        }
    } catch (SQLException e) {
        e.addSuppressed(e);
    } 
    return uitleningen;

It might be nothing, but it almost looks like the error only occurs when I run 2 queries almost immediately after each other. Is it possible that closing the connection takes a while?

Chances are that you run into the database connection problem because your code does not properly close the database connections as well as the statements and result sets.

A statement will also close its active result set. Most JDBC will also close the statement if the connection is closed.

So closing the connection is the most important part. It cannot be achieved with your current code structure because you create it in an inner method and do not return it.

It has also been mentioned that the exception handling is poor because you ignore exceptions and return null instead causing seemingly unrelated crashes later. In many cases it might be easier to declare that the method throws SQLException .

You might want to change your code like so:

List<Uitlening> retrieveData() {
    final String query = "SELECT * FROM uitlening";
    try (Connection connection = DriverManager.getConnection(url, login, password);
         Statement statement = connection.createStatement();
         ResultSet resultSet = statement.executeQuery(query)) {
        return processResultSet(resultSet);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

List<Uitlening> processResultSet(ResultSet resultSet) throws SQLException {
    List<Uitlening> uitleningen = new ArrayList<>();
    while (resultSet.next()) {
        Uitlening uitlening = new Uitlening();
        uitlening.setNr(resultSet.getInt("nr"));
        uitleningen.add(uitlening);
    }
    return uitleningen;
}

It closes the connection, the statement and the result set by using try/catch blocks that take advantage of AutoClosable s (in this case: Connection , Statement , ResultSet ).

The method processResultSet declares the SQLException so it doesn't need to handle it.

The code is rearrange so the data is fully processed before the code leaves the try/catch block that closes the connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM