Web hosts - CHAPTER 10 Transactions and concurrency This example is
CHAPTER 10 Transactions and concurrency This example is simplified, but it s enough to illustrate how a unit of work that mixes entity and scalar reads is vulnerable to nonrepeatable reads, if the database transaction isolation level is read committed. Instead of switching all database transactions into a higher and nonscalable isolation level, you obtain stronger isolation guarantees when necessary with the lock() method on the Hibernate Session: Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Item i = (Item) session.get(Item.class, 123); session.lock(i, LockMode.UPGRADE); String description = (String) session.createQuery(”select i.description from Item i” + ” where i.id = :itemid”) .setParameter(”itemid”, i.getId() ) .uniqueResult(); tx.commit(); session.close(); Using LockMode.UPGRADE results in a pessimistic lock held on the database for the row(s) that represent the Item instance. Now no concurrent transaction can obtain a lock on the same data that is, no concurrent transaction can modify the data between your two reads. This can be shortened as follows: Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Item i = (Item) session.get(Item.class, 123, LockMode.UPGRADE); … A LockMode.UPGRADE results in an SQL SELECT … FOR UPDATE or similar, depending on the database dialect. A variation, LockMode.UPGRADE_NOWAIT, adds a clause that allows an immediate failure of the query. Without this clause, the database usually waits when the lock can t be obtained (perhaps because a concurrent transaction already holds a lock). The duration of the wait is database-dependent, as is the actual SQL clause. FAQ Can I use long pessimistic locks? The duration of a pessimistic lock in Hibernate is a single database transaction. This means you can t use an exclusive lock to block concurrent access for longer than a single database transaction. We consider this a good thing, because the only solution would be an extremely expensive lock held in memory (or a so-called lock table in the database) for the duration of, for example, a whole conversation. These kinds of locks are sometimes called offline
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.