Affordable web hosting - Bulk and batch operations 12.2.2 Processing with batches

Bulk and batch operations 12.2.2 Processing with batches Imagine that you have to manipulate all Item objects, and that the changes you have to make aren t as trivial as setting a flag (which you ve done with a single statement previously). Let s also assume that you can t create an SQL stored procedure, for whatever reason (maybe because your application has to work on database- management systems that don t support stored procedures). Your only choice is to write the procedure in Java and to retrieve a massive amount of data into memory to run it through the procedure. You should execute this procedure by batching the work. That means you cre ate many smaller datasets instead of a single dataset that wouldn t even fit into memory. Writing a procedure with batch updates The following code loads 100 Item objects at a time for processing: Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); ScrollableResults itemCursor = session.createQuery(”from Item”).scroll(); int count=0; while ( itemCursor.next() ) { Item item = (Item) itemCursor.get(0); modifyItem(item); if ( ++count % 100 == 0 ) { session.flush(); session.clear(); } } tx.commit(); session.close(); You use an HQL query (a simple one) to load all Item objects from the database. But instead of retrieving the result of the query completely into memory, you open an online cursor. A cursor is a pointer to a result set that stays in the database. You can control the cursor with the ScrollableResults object and move it along the result. The get(int i) call retrieves a single object into memory, the object the cursor is currently pointing to. Each call to next() forwards the cursor to the next object. To avoid memory exhaustion, you flush() and clear() the persistence context before loading the next 100 objects into it.
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

Leave a Reply