Why do I need Transaction in Hibernate for read-only operations?

Transactions for reading might look indeed strange and often people don’t mark methods for transactions in this case. But JDBC will create transaction anyway, it’s just it will be working in autocommit=true if different option wasn’t set explicitly. But there are practical reasons to mark transactions read-only: Impact on databases Read-only flag may let DBMS … Read more

Laravel: Using try…catch with DB::transaction()

In the case you need to manually ‘exit’ a transaction through code (be it through an exception or simply checking an error state) you shouldn’t use DB::transaction() but instead wrap your code in DB::beginTransaction and DB::commit/DB::rollback(): DB::beginTransaction(); try { DB::insert(…); DB::insert(…); DB::insert(…); DB::commit(); // all good } catch (\Exception $e) { DB::rollback(); // something went … Read more

Correct use of flush() in JPA/Hibernate

Probably the exact details of em.flush() are implementation-dependent. In general anyway, JPA providers like Hibernate can cache the SQL instructions they are supposed to send to the database, often until you actually commit the transaction. For example, you call em.persist(), Hibernate remembers it has to make a database INSERT, but does not actually execute the … Read more

MySQL: Transactions vs Locking Tables

Locking tables prevents other DB users from affecting the rows/tables you’ve locked. But locks, in and of themselves, will NOT ensure that your logic comes out in a consistent state. Think of a banking system. When you pay a bill online, there’s at least two accounts affected by the transaction: Your account, from which the … Read more

javax.transaction.Transactional vs org.springframework.transaction.annotation.Transactional

Spring has defined its own Transactional annotation to make Spring bean methods transactional, years ago. Java EE 7 has finally done the same thing and now allows CDI bean methods to be transactional, in addition to EJB methods. So since Java EE 7, it also defines its own Transactional annotation (it obviously can’t reuse the … Read more

How to work around the lack of transactions in MongoDB?

As of 4.0, MongoDB will have multi-document ACID transactions. The plan is to enable those in replica set deployments first, followed by the sharded clusters. Transactions in MongoDB will feel just like transactions developers are familiar with from relational databases – they’ll be multi-statement, with similar semantics and syntax (like start_transaction and commit_transaction). Importantly, the … Read more

Transactions in .net

There are 2 main kinds of transactions; connection transactions and ambient transactions. A connection transaction (such as SqlTransaction) is tied directly to the db connection (such as SqlConnection), which means that you have to keep passing the connection around – OK in some cases, but doesn’t allow “create/use/release” usage, and doesn’t allow cross-db work. An … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)