Package javax.transaction

Examples of javax.transaction.UserTransaction


   {
      try
      {
         InitialContext ctx = new InitialContext();
         Queue queue = (Queue) ctx.lookup("queue/testQueue");
         UserTransaction ut = sessionCtx.getUserTransaction();
         ut.setTransactionTimeout(5);
         ut.begin();
         try
         {
            ConnectionFactory cf = (ConnectionFactory) ctx.lookup("java:JmsXA");
            javax.jms.Connection c = cf.createConnection();
            try
            {
               c.start();
               Session s = c.createSession(true, Session.SESSION_TRANSACTED);
               MessageConsumer mc = s.createConsumer(queue);
               mc.receiveNoWait();
               mc.close();
               try
               {
                  Thread.sleep(10000);
               }
               catch (InterruptedException ignored)
               {
               }
              
               try
               {
                  MessageProducer p = s.createProducer(queue);

                  Message m = s.createTextMessage("100");
                  p.send(m);
               }
               catch (JMSException expected)
               {
               }
            }
            finally
            {
               try
               {
                  c.close();
               }
               catch (Exception ignored)
               {
               }
            }
         }
         finally
         {
            try
            {
               ut.commit();
            }
            catch (Exception ignored)
            {
            }
         }
View Full Code Here


   {
      try
      {
         InitialContext ctx = new InitialContext();
         Queue queue = (Queue) ctx.lookup("queue/testQueue");
         UserTransaction ut = sessionCtx.getUserTransaction();
         ut.begin();
         try
         {
            ConnectionFactory cf = (ConnectionFactory) ctx.lookup("java:JmsXA");
            javax.jms.Connection c = cf.createConnection();
            try
            {
               c.start();
               Session s = c.createSession(true, Session.SESSION_TRANSACTED);
               MessageConsumer mc = s.createConsumer(queue);
               Message m = mc.receive(1000);
               if (m == null || m instanceof TextMessage == false)
                  throw new RuntimeException("Expected one text message: " + m);
               String value = ((TextMessage) m).getText();
               if ("101".equals(value) == false)
                  throw new RuntimeException("Message should have text 101 got: " + value);
               if (mc.receive(1000) != null)
                  throw new RuntimeException("Did not expect two messages");
            }
            finally
            {
               try
               {
                  c.close();
               }
               catch (Exception ignored)
               {
               }
            }
         }
         finally
         {
            try
            {
               ut.commit();
            }
            catch (Exception ignored)
            {
            }
         }
View Full Code Here

   public void testWithDefaultJndiContext() throws Exception
   {
      System.setProperty(Context.PROVIDER_URL, getServerHost() + ":1099");
      InitialContext ctx = new InitialContext();
      UserTransaction ut = (UserTransaction) ctx.lookup("UserTransaction");

      ut.begin();
      try
      {
         ut.begin();
         fail("Attempt to start a nested user transaction should fail with NotSupportedException.");
      }
      catch(NotSupportedException e)
      {
         // expected
      }
      finally
      {
         ut.rollback();
      }
   }
View Full Code Here

      public UserTransaction getUserTransaction()
      {
         AllowedOperationsAssociation.assertAllowedIn("getUserTransaction",
                 IN_EJB_CREATE | IN_EJB_REMOVE | IN_BUSINESS_METHOD | IN_EJB_TIMEOUT | IN_SERVICE_ENDPOINT_METHOD);
         final UserTransaction ut = super.getUserTransaction();
         return new UserTransaction()
         {
            public void begin() throws NotSupportedException, SystemException
            {
               checkUserTransactionMethods();
               ut.begin();
            }

            public void commit() throws RollbackException,
               HeuristicMixedException,
               HeuristicRollbackException,
               SecurityException,
               IllegalStateException,
               SystemException
            {
               checkUserTransactionMethods();
               ut.commit();
            }

            public void rollback() throws IllegalStateException, SecurityException, SystemException
            {
               checkUserTransactionMethods();
               ut.rollback();
            }

            public void setRollbackOnly() throws IllegalStateException, SystemException
            {
               checkUserTransactionMethods();
               ut.setRollbackOnly();
            }

            public int getStatus() throws SystemException
            {
               checkUserTransactionMethods();
               return ut.getStatus();
            }

            public void setTransactionTimeout(int seconds) throws SystemException
            {
               checkUserTransactionMethods();
               ut.setTransactionTimeout(seconds);
            }
         };
      }
View Full Code Here

   {
   }

   public void doNormal() throws RemoteException
   {
      UserTransaction ut = sessionCtx.getUserTransaction();
      try
      {
         ut.begin();
         ut.commit();
      }
      catch (Exception e)
      {
         throw new RemoteException("Error", e);
      }
View Full Code Here

      remote.doNormal();
   }

   public void doIncomplete() throws RemoteException
   {
      UserTransaction ut = sessionCtx.getUserTransaction();
      try
      {
         ut.begin();
      }
      catch (Exception e)
      {
         throw new RemoteException("Error", e);
      }
View Full Code Here

      remote.doNormal();
   }

   public void doTimeout() throws RemoteException
   {
      UserTransaction ut = sessionCtx.getUserTransaction();
      try
      {
         ut.setTransactionTimeout(5);
         ut.begin();
         Thread.sleep(10000);
      }
      catch (InterruptedException ignored)
      {
      }
View Full Code Here

{  

   @Create
   public void create()
   {
      UserTransaction t = null;
      try
      {
         InitialContext ctx = new InitialContext();
        
         t = (UserTransaction) ctx.lookup("UserTransaction");
         t.begin();
     
         EntityManager em = (EntityManager) Component.getInstance("entityManager", true);
        
         List<Auction> auctions = em.createQuery("select a from Auction a").getResultList();
        
         Calendar cal = new GregorianCalendar();
        
         Random r = new Random(System.currentTimeMillis());
        
         for (Auction auction : auctions)
         {
            cal.setTime(auction.getEndDate());
            cal.add(Calendar.DATE, r.nextInt(7));
            cal.add(Calendar.MINUTE, 30 + r.nextInt(1410));
            auction.setEndDate(cal.getTime());
            auction.setStatus(Auction.STATUS_COMPLETED);
            em.merge(auction);

            AuctionEndAction auctionEnd = (AuctionEndAction) Component.getInstance(AuctionEndAction.class, true);
            auctionEnd.endAuction(auction.getAuctionId(), auction.getEndDate());
         }
        
         t.commit();
      }
      catch (Exception e)
      {
         try
         {
            if (t != null)
               t.rollback();
         }
         catch (SystemException e1) {}
        
         throw new RuntimeException("Error starting transaction", e);
      }     
View Full Code Here

    @Asynchronous
    public void rebuildIndex(Class entityClass, Progress progress) {

        log.info("asynchronously rebuilding Lucene index for entity: " + entityClass);

        UserTransaction userTx = null;

        try {
            progress.setStatus("Purging index");
            log.debug("deleting indexed documents of entity: " + entityClass.getName());
            userTx = (UserTransaction)org.jboss.seam.Component.getInstance("org.jboss.seam.transaction.transaction");
            userTx.begin();

            EntityManager em = (EntityManager) Component.getInstance("entityManager");
            FullTextSession ftSession = (FullTextSession)em.getDelegate();

            // Delete all documents with "_hibernate_class" term of the selected entity
            DirectoryProvider dirProvider = ftSession.getSearchFactory().getDirectoryProviders(entityClass)[0];
            IndexReader reader = IndexReader.open(dirProvider.getDirectory());

            // TODO: This is using an internal term of HSearch
            reader.deleteDocuments(new Term("_hibernate_class", entityClass.getName()));
            reader.close();

            // Optimize index
            progress.setStatus("Optimizing index");
            log.debug("optimizing index (merging segments)");
            ftSession.getSearchFactory().optimize(entityClass);

            userTx.commit();

            progress.setStatus("Building index");
            log.debug("indexing documents in batches of: " + batchSize);

            // Now re-index with HSearch
View Full Code Here

public class Aargh
{  
   @Create
   public void create()
   {
      UserTransaction t = null;
      try
      {
         InitialContext ctx = new InitialContext();
        
         t = (UserTransaction) ctx.lookup("UserTransaction");
         t.begin();
     
         EntityManager em = (EntityManager) Component.getInstance("entityManager", true);
        
         List<Auction> auctions = em.createQuery("from Auction").getResultList();
        
         Calendar cal = new GregorianCalendar();
        
         Random r = new Random(System.currentTimeMillis());
        
         for (Auction auction : auctions)
         {
            cal.setTime(auction.getEndDate());
            cal.add(Calendar.DATE, r.nextInt(7));
            cal.add(Calendar.MINUTE, 30 + r.nextInt(1410));
            auction.setEndDate(cal.getTime());
            em.merge(auction);
           
            AuctionEndAction auctionEnd = (AuctionEndAction) Component.getInstance(AuctionEndAction.class, true);
            auctionEnd.endAuction(auction.getAuctionId(), auction.getEndDate());
         }
        
         t.commit();
      }
      catch (Exception e)
      {
         try
         {
            if (t != null)
               t.rollback();
         }
         catch (SystemException e1) {}
        
         throw new RuntimeException("Error starting transaction", e);
      }     
View Full Code Here

TOP

Related Classes of javax.transaction.UserTransaction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.