java - JDBC - setAutoCommit for read only operation -
let's have common method creates db connection:
connection getconnection() throws sqlexception { connection con = ... // create connection con.setautocommit(false); return con; }
i put setautocommit(false)
call here callers of method never have worry setting it. however, bad practice if operation executed caller reading data? there overhead?
my personal opinion it's better centralize logic in 1 place, way callers never have set auto commit , avoids code redundancy. wanted make sure didn't incur unnecessary overhead read operation.
i put setautocommit(false) call here callers of method never have worry setting it.
this fine imo , believe 1 should never ever enable auto-commit mode inside application. recommendation turn off auto-commit.
however, bad practice if operation executed caller reading data? there overhead?
from strict performance point of view, it's starting , ending database transaction every sql statement has overhead , may decrease performance of application.
by way, select statements affected setautocommit(boolean)
according javadoc:
sets connection's auto-commit mode given state. if connection in auto-commit mode, sql statements executed , committed individual transactions. otherwise, sql statements grouped transactions terminated call either method commit or method rollback. default, new connections in auto-commit mode.
the commit occurs when statement completes. time when statement completes depends on type of sql statement:
- for dml statements, such insert, update or delete, , ddl statements, statement complete has finished executing.
- for select statements, statement complete when associated result set closed.
- for callablestatement objects or statements return multiple results, statement complete when of associated result sets have been closed, , update counts , output parameters have been retrieved.
Comments
Post a Comment