- Mine
- Wednesday, August 29th, 2007 at 1:02:53am MDT
- package net.oxbeef.transaction;
- import java.util.Collection;
- import java.util.Iterator;
- import javax.ejb.Remote;
- import javax.ejb.Stateless;
- import javax.ejb.TransactionAttribute;
- import javax.ejb.TransactionAttributeType;
- import javax.persistence.EntityManager;
- import javax.persistence.PersistenceContext;
- @Stateless
- @Remote(DriverRemote.class)
- public class Driver implements DriverRemote {
- @PersistenceContext
- protected EntityManager em;
- // MAIN ENTRY POINT
- @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
- public void growLegs() throws Exception {
- Collection c = em.createQuery("from Tiger t order by t.numLegs desc")
- .getResultList();
- Tiger t;
- int size = c.size();
- for (Iterator i = c.iterator(); i.hasNext();) {
- t = (Tiger) i.next();
- t.setNumLegs(t.getNumLegs() + 1);
- // 50% chance of fail overall
- double prob = Math.pow(0.5, 1. / size);
- if (Math.random() > prob) {
- // Emulated failure causes rollback
- // throw new RollingException();
- try {
- forbidden(t);
- } catch( Exception e ) {
- throw new RollingException();
- }
- }
- // Synchronize the persistence context to the DB
- // All flushed updates will be removed from the DB
- // if the transaction is rolled back
- em.flush();
- }
- }
- // execution has 50% chance of reaching here. It does get here, but doesn't fail.
- @TransactionAttribute(TransactionAttributeType.NEVER)
- protected void forbidden(Tiger t) {
- System.out.println("trying to do something here");
- t.setNumLegs(0);
- em.flush();
- // why didn't it fail with an EJBException??
- }
- // just return a list of what's in the db
- public int[] getValues() {
- int[] list;
- try {
- Collection c = em.createQuery(
- "from Tiger t order by t.numLegs desc").getResultList();
- list = new int[c.size()];
- int count = 0;
- for (Iterator i = c.iterator(); i.hasNext();) {
- list[count++] = ((Tiger) i.next()).getNumLegs();
- }
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- return list;
- }
- }
advertising
Update the Post
Either update this post and resubmit it with changes, or make a new post.
You may also comment on this post.
Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.