¿Necesito escribir todo el acceso a la base de datos como una transacción como esta?

To ensure the safty, do I need to write all the database accessing code like this:

public void updateOrder(OrderData order) throw Exception {
            Statement stmt = conn.createStatement();
            String sql = "update order set .... where ";
            try {
            } catch (Exception e) {
            } finally {



In this example, I have only access to one table, do I still need to treat it as transaction? Do I need to write all the database access as transaction like this?

preguntado el 08 de noviembre de 11 a las 09:11

2 Respuestas

That depends if there are multiple sql updates and inserts or not. If you are executing a single update or insert it will implicitly run within a transaction.

respondido 08 nov., 11:13

Any statement in JDBC is implicitly executed in a transaction, the only difference is the behavior of autocommit false <> true: false: statement does not commit itself and statements only start a new transaction if no transaction is active; true: statement does commit itself when completed and next statement starts a new transaction. - Mark Rotteveel

In autoCommit, the database (or JDBC driver) will start and commit transactions for individually executed statements. So if this is the only statement to be executed, then you do not need to set autoCommit to false and you also don't need to handle the commit or rollback yourself.

On the other hand if you want to execute multiple statements that should commit or rollback atomically, then you do need to disable auto commit.

respondido 08 nov., 11:15

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.