Is it Ok to Pass ResultSet?

后端 未结 2 1308
栀梦
栀梦 2020-12-10 14:44

In my situation, I am querying a database for a specific return (in this case registration information based on a username).

            //Build SQL String a         


        
相关标签:
2条回答
  • 2020-12-10 15:17

    Technically, it's OK to pass result sets, as long as you are not serializing and passing it to a different JVM, and your JDBC connection and statement are still open.

    However, it's probably a better software engineer and programming practice to have DB access layer that returns you the result set in a Java encoded way (a list of User in your example). That way, your code would be cleaner and you won't have to worry if the ResultSet is already opened, or you have to scroll it to the top, you name it...

    0 讨论(0)
  • 2020-12-10 15:19

    As everyone before me said its a bad idea to pass the result set. If you are using Connection pool library like c3p0 then you can safely user CachedRowSet and its implementation CachedRowSetImpl. Using this you can close the connection. It will only use connection when required. Here is snippet from the java doc:

    A CachedRowSet object is a disconnected rowset, which means that it makes use of a connection to its data source only briefly. It connects to its data source while it is reading data to populate itself with rows and again while it is propagating changes back to its underlying data source. The rest of the time, a CachedRowSet object is disconnected, including while its data is being modified. Being disconnected makes a RowSet object much leaner and therefore much easier to pass to another component. For example, a disconnected RowSet object can be serialized and passed over the wire to a thin client such as a personal digital assistant (PDA).

    Here is the code snippet for querying and returning ResultSet:

    public ResultSet getContent(String queryStr) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet resultSet = null;
        CachedRowSetImpl crs = null;
        try {
            Connection conn = dataSource.getConnection();
            stmt = conn.createStatement();
            resultSet = stmt.executeQuery(queryStr);
    
            crs = new CachedRowSetImpl();
            crs.populate(resultSet);
        } catch (SQLException e) {
            throw new IllegalStateException("Unable to execute query: " + queryStr, e);
        }finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                LOGGER.error("Ignored", e);
            }
        }
    
        return crs;
    }
    

    Here is the snippet for creating data source using c3p0:

     ComboPooledDataSource cpds = new ComboPooledDataSource();
                try {
                    cpds.setDriverClass("<driver class>"); //loads the jdbc driver
                } catch (PropertyVetoException e) {
                    e.printStackTrace();
                    return;
                }
                cpds.setJdbcUrl("jdbc:<url>");
                cpds.setMinPoolSize(5);
                cpds.setAcquireIncrement(5);
                cpds.setMaxPoolSize(20);
    
     javax.sql.DataSource dataSource = cpds;
    
    0 讨论(0)
提交回复
热议问题