Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Mine
Wednesday, August 29th, 2007 at 1:02:53am MDT 

  1. package net.oxbeef.transaction;
  2.  
  3. import java.util.Collection;
  4. import java.util.Iterator;
  5.  
  6. import javax.ejb.Remote;
  7. import javax.ejb.Stateless;
  8. import javax.ejb.TransactionAttribute;
  9. import javax.ejb.TransactionAttributeType;
  10. import javax.persistence.EntityManager;
  11. import javax.persistence.PersistenceContext;
  12.  
  13. @Stateless
  14. @Remote(DriverRemote.class)
  15. public class Driver implements DriverRemote {
  16.  
  17.         @PersistenceContext
  18.         protected EntityManager em;
  19.  
  20. // MAIN ENTRY POINT
  21.         @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
  22.         public void growLegs() throws Exception {
  23.                 Collection c = em.createQuery("from Tiger t order by t.numLegs desc")
  24.                                 .getResultList();
  25.                 Tiger t;
  26.                 int size = c.size();
  27.                 for (Iterator i = c.iterator(); i.hasNext();) {
  28.                         t = (Tiger) i.next();
  29.                         t.setNumLegs(t.getNumLegs() + 1);
  30.  
  31.                         // 50% chance of fail overall
  32.                         double prob = Math.pow(0.5, 1. / size);
  33.                         if (Math.random() > prob) {
  34.                                 // Emulated failure causes rollback
  35. //                        throw new RollingException();
  36.                                 try {
  37.                                         forbidden(t);
  38.                                 } catch( Exception e ) {
  39.                                         throw new RollingException();
  40.                                 }
  41.                         }
  42.                         // Synchronize the persistence context to the DB
  43.                         // All flushed updates will be removed from the DB
  44.                         // if the transaction is rolled back
  45.                         em.flush();
  46.                 }
  47.  
  48.         }
  49.  
  50. // execution has 50% chance of reaching here. It does get here, but doesn't fail.
  51.         @TransactionAttribute(TransactionAttributeType.NEVER)
  52.         protected void forbidden(Tiger t) {
  53.                 System.out.println("trying to do something here");
  54.                 t.setNumLegs(0);
  55.                 em.flush();
  56.                 // why didn't it fail with an EJBException??
  57.         }
  58.        
  59.  
  60. // just return a list of what's in the db
  61.         public int[] getValues() {
  62.                 int[] list;
  63.                 try {
  64.                         Collection c = em.createQuery(
  65.                                         "from Tiger t order by t.numLegs desc").getResultList();
  66.                         list = new int[c.size()];
  67.                         int count = 0;
  68.                         for (Iterator i = c.iterator(); i.hasNext();) {
  69.                                 list[count++] = ((Tiger) i.next()).getNumLegs();
  70.                         }
  71.                 } catch (Exception e) {
  72.                         e.printStackTrace();
  73.                         return null;
  74.                 }
  75.                 return list;
  76.         }
  77. }

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.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



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.

worth-right
fantasy-obligation
fantasy-obligation