Proper way to handle SQL database connections in java OOP style

jeffery_the_wind

I recently did a programming challenge. It was an OOP challenge to expose an API for managing a fictitious movie rental company. I chose to use sparkjava, and worked from a heroku template. I have posted the code to Github here, and invite anyone to query the URL provided in the README doc. The secret password is secret.

I received a lot of good feedback about things that need improvement. One objection is:

Handling connections directly vi the driver, without any handling for closing connections properly.

So I am trying to figure out what this means, why its a problem, and how to fix it and make my code better.

For example I have this method, in the main class:

public static int validate_customer(String cust) throws SQLException, URISyntaxException {
    int customer = 0;
    try{
        customer = Integer.parseInt(cust);
    }catch (Exception e){
        throw new SQLException("Invalid customer integer -> " + cust);
    }

    Connection connection = DatabaseUrl.extract().getConnection();
    PreparedStatement stmt = connection.prepareStatement("SELECT count(*) from customers where id = ?;");
    stmt.setInt(1, customer);
    ResultSet rs = stmt.executeQuery();
    rs.next();
    if ( rs.getInt(1) < 1 ){
        throw new SQLException("Invalid customer id -> " + customer);
    }
    rs.close();
    stmt.close();
    return customer;
}

Why is this an incorrect way to handle interaction with the DB? There are other methods there in the main class which interact with the DB but the technique is basically the same as this example.

To me, if there was a problem with the connection to the database, it would occur in the line: Connection connection = DatabaseUrl.extract().getConnection(); and throw an SQLException. If somethings happens to the connection, and some part of the query doesn't work, an exception will be thrown from this validate_customer method. Why is this a problem in my code?

codefinger

Handling connections directly vi the driver

This means you are not using a DB connection pool (like HikariCP), which means it's possible exhaust your connection limit very quickly. The Heroku example does not use a DB connection pool to limit dependencies in the example.

without any handling for closing connections properly.

This means the stmt.close(); is not a in a finally block, which means if an exception is thrown in the method the stmt will never be closed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Right way to handle database access in PHP OOP

From Dev

What is the proper way to handle multiple clients connections on linux server

From Dev

Proper way to handle the code

From Dev

What is the proper way to wait for connections?

From Dev

What is the proper way to wait for connections?

From Dev

Java : Proper way a method() should handle a bad call

From Dev

Java : Proper way a method() should handle a bad call

From Dev

Proper way to manipulate database

From Dev

Proper way to handle scope in JavaScript

From Dev

Proper way to handle a fail to init

From Dev

Proper way to handle library dependencies

From Dev

Proper way to handle a fail to init

From Dev

Proper way to handle library dependencies

From Dev

Proper way to handle scope in JavaScript

From Dev

Proper way to maintain many connections with Celluloid?

From Dev

Proper way to maintain many connections with Celluloid?

From Dev

Using proper OOP patterns to build a game in Java

From Dev

proper OOP and flexibility between classes in java

From Dev

Using proper OOP patterns to build a game in Java

From Dev

Proper way to migrate a postgres database?

From Dev

Proper way to store json in database

From Dev

Looking for a way to handle/approach PHP OOP websites

From Dev

proper way to handle these RESTful calls for handling state

From Java

What is the proper way to handle data passing in Redux

From Dev

What is the proper way to handle OpenCL buffers of structs?

From Dev

What is the proper way to handle string encoding in haskell?

From Dev

What is the proper way to handle remote server disconnection?

From Dev

Proper way to handle multiple "pages" in meteor

From Dev

Proper way to handle enum with associated strings

Related Related

  1. 1

    Right way to handle database access in PHP OOP

  2. 2

    What is the proper way to handle multiple clients connections on linux server

  3. 3

    Proper way to handle the code

  4. 4

    What is the proper way to wait for connections?

  5. 5

    What is the proper way to wait for connections?

  6. 6

    Java : Proper way a method() should handle a bad call

  7. 7

    Java : Proper way a method() should handle a bad call

  8. 8

    Proper way to manipulate database

  9. 9

    Proper way to handle scope in JavaScript

  10. 10

    Proper way to handle a fail to init

  11. 11

    Proper way to handle library dependencies

  12. 12

    Proper way to handle a fail to init

  13. 13

    Proper way to handle library dependencies

  14. 14

    Proper way to handle scope in JavaScript

  15. 15

    Proper way to maintain many connections with Celluloid?

  16. 16

    Proper way to maintain many connections with Celluloid?

  17. 17

    Using proper OOP patterns to build a game in Java

  18. 18

    proper OOP and flexibility between classes in java

  19. 19

    Using proper OOP patterns to build a game in Java

  20. 20

    Proper way to migrate a postgres database?

  21. 21

    Proper way to store json in database

  22. 22

    Looking for a way to handle/approach PHP OOP websites

  23. 23

    proper way to handle these RESTful calls for handling state

  24. 24

    What is the proper way to handle data passing in Redux

  25. 25

    What is the proper way to handle OpenCL buffers of structs?

  26. 26

    What is the proper way to handle string encoding in haskell?

  27. 27

    What is the proper way to handle remote server disconnection?

  28. 28

    Proper way to handle multiple "pages" in meteor

  29. 29

    Proper way to handle enum with associated strings

HotTag

Archive