gmustudent
gmustudent

Reputation: 2209

Prepared Statement, Batch Update, Different Updates

Is it possible to do a batch update but have different statements? Every tutorial and online resource I find uses the same statement. This is what I have but it's not working.

public static void resetNotifications(User user)
{
    DataSource dataSource = null;
    Context initialContext = null;
    Context environmentContext = null;
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    String stride = "UPDATE stride SET recipientView = 1 WHERE recipientId = ?";
    String strideLike = "UPDATE strideLike SET recipientView = 1 WHERE recipientId = ? ";
    String strideCommentMe = "UPDATE strideComment SET recipientView = 1 WHERE recipientId = ? ";
    String strideCommentLike = "UPDATE strideCommentLike SET recipientView = 1 WHERE recipientId = ? ";
    String strideCommentFollow = "UPDATE strideCommentNotification SET recipientView = 1 WHERE userId = ? ";

    try
    {
        initialContext = new InitialContext();
        environmentContext = (Context) initialContext.lookup("java:/comp/env");
        dataSource = (DataSource) environmentContext.lookup("jdbc/instride");
        connection = dataSource.getConnection();
        connection.setAutoCommit(false);

        preparedStatement = connection.prepareStatement(stride);
        preparedStatement.setLong(1, user.getId());
        preparedStatement.addBatch();

        preparedStatement = connection.prepareStatement(strideLike);
        preparedStatement.setLong(1, user.getId());
        preparedStatement.addBatch();

        preparedStatement = connection.prepareStatement(strideCommentMe);
        preparedStatement.setLong(1, user.getId());
        preparedStatement.addBatch();

        preparedStatement = connection.prepareStatement(strideCommentLike);
        preparedStatement.setLong(1, user.getId());
        preparedStatement.addBatch();

        preparedStatement = connection.prepareStatement(strideCommentFollow);
        preparedStatement.setLong(1, user.getId());
        preparedStatement.addBatch();

        preparedStatement.executeBatch();
        connection.commit();
    }
    catch(NamingException error)
    {
        System.out.println("NotificationsDAO - resetNotifications() - Error With JNDI Lookup - " + error.getMessage());
        error.printStackTrace();
    }
    catch(SQLException error)
    {
        System.out.println("NotificationsDAO - resetNotifications() - Error With SQL - " + error.getMessage());
        error.printStackTrace();
    }
    finally
    {
        if (initialContext != null) try{initialContext.close();} catch(NamingException ignore) {}
        if (environmentContext != null) try{environmentContext.close();} catch(NamingException ignore) {}
        if (connection != null) try{connection.close();} catch(SQLException ignore) {}
        if (preparedStatement != null) try{preparedStatement.close();} catch(SQLException ignore) {}
    }
}

Upvotes: 4

Views: 4327

Answers (2)

zagyi
zagyi

Reputation: 17518

It is possible to execute multiple different statements in one batch if you use a simple Statement instead of different PrepareStatements. See documentation and examples here.

Upvotes: 0

1218985
1218985

Reputation: 8012

You cannot execute multiple different statements in a single batch but, you can use a stored procedure that can do it all in a single roundtrip to the server while maintaining the benefits of single transaction

Upvotes: 1

Related Questions