public void onMessage(final Message message) { try { // Step 9. We know the client is sending a text message so we cast TextMessage textMessage = (TextMessage) message; // Step 10. get the text from the message. String text = textMessage.getText(); System.out.println("message " + text + " received"); if (!textMessage.getJMSRedelivered()) { // Step 11. On first delivery get the transaction, take a look, and throw an exception Transaction tx = tm.getTransaction(); if (tx != null) { System.out.println("something is wrong, there should be no global transaction: " + tx); } else { System.out.println( "there is no global transaction, although the message delivery is using a local transaction"); System.out.println("let's throw an exception and see what happens"); throw new RuntimeException("DOH!"); } } else { // Step 12. Print the message System.out.println( "The message was redelivered since the message delivery used a local transaction"); } } catch (JMSException e) { e.printStackTrace(); } catch (SystemException e) { e.printStackTrace(); } }
void setRollbackOnly() { try { transactionManager.setRollbackOnly(); } catch (javax.transaction.SystemException se) { se.printStackTrace(); } }
public static void main(String[] args) { UserTransaction ut = null; try { boolean abort = false; System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); System.setProperty(Context.PROVIDER_URL, "t3://localhost:7001"); ut = getUserTransaction(); ut.begin(); MessagePublisher mp = new MessagePublisher("jms/JTATestJMSConnectionFactory", "jms/JTATestJMSQueue", false); mp.publishMessage("Hello JMS2"); mp.publishMessage("Hello again JMS2"); new AmitkJTADao("jdbc/JTATestDataSource") .updateRecord(1, new Date(System.currentTimeMillis())); if (abort) throw new Exception("Forcefully aborting transaction"); ut.commit(); } catch (Exception e) { e.printStackTrace(); try { if (null != ut) ut.rollback(); } catch (SystemException se) { se.printStackTrace(); } finally { ut = null; } } }
/** 重载父类方法,设置事务的状态标识为回滚状态 */ public void rollback() throws SQLException { JDBCTransaction tx = TransactionManager.getTransaction(); try { if (tx != null) tx.setRollbackOnly(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SystemException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { String showid = request.getParameter("si"); String[] pics; pics = request.getParameterValues("pics[]"); String[] comms; comms = request.getParameterValues("comms[]"); utx.begin(); EntityManager em = emf.createEntityManager(); for (int i = 0; i < pics.length; i++) { Ecpresentations etemp = em.getReference(Ecpresentations.class, showid); Ecinfo ef = new Ecinfo(); ef.setPicy(pics[i]); ef.setDetails(comms[i]); ef.setShowid(etemp); em.persist(ef); } utx.commit(); RequestDispatcher view = request.getRequestDispatcher("addinforeport.jsp"); view.forward(request, response); em.close(); } catch (NotSupportedException ex) { ex.printStackTrace(); } catch (SystemException ex) { ex.printStackTrace(); } catch (RollbackException ex) { ex.printStackTrace(); } catch (HeuristicMixedException ex) { ex.printStackTrace(); } catch (HeuristicRollbackException ex) { ex.printStackTrace(); } catch (SecurityException ex) { ex.printStackTrace(); } catch (IllegalStateException ex) { ex.printStackTrace(); } }
@PostConstruct private void init() { try { userTransaction.begin(); initData(); userTransaction.commit(); } catch (NotSupportedException e) { e.printStackTrace(); } catch (SystemException e) { e.printStackTrace(); } catch (HeuristicRollbackException e) { e.printStackTrace(); } catch (javax.transaction.RollbackException e) { e.printStackTrace(); } catch (HeuristicMixedException e) { e.printStackTrace(); } }
/** @see com.idega.block.importer.business.ImportFileHandler#handleRecords() */ @Override public boolean handleRecords() { failedRecords = new ArrayList(); errorLog = new TreeMap(); report = new Report(file.getFile().getName()); // Create a report file. // It will be located in // the Report dir IWTimestamp t = IWTimestamp.RightNow(); t.setAsDate(); today = t.getDate(); t.setDay(1); firstDayInCurrentMonth = t.getTimestamp(); t.addDays(-1); lastDayInPreviousMonth = t.getTimestamp(); transaction = this.getSessionContext().getUserTransaction(); Timer clock = new Timer(); clock.start(); try { // initialize business beans and data homes communeUserBusiness = (CommuneUserBusiness) this.getServiceInstance(CommuneUserBusiness.class); schoolBusiness = (SchoolBusiness) this.getServiceInstance(SchoolBusiness.class); schoolHome = schoolBusiness.getSchoolHome(); schoolTypeHome = schoolBusiness.getSchoolTypeHome(); schoolYearHome = schoolBusiness.getSchoolYearHome(); schoolClassHome = (SchoolClassHome) this.getIDOHome(SchoolClass.class); schoolClassMemberHome = (SchoolClassMemberHome) this.getIDOHome(SchoolClassMember.class); communeHome = (CommuneHome) this.getIDOHome(Commune.class); studyPathHome = (SchoolStudyPathHome) this.getIDOHome(SchoolStudyPath.class); placementImportDateHome = (PlacementImportDateHome) this.getIDOHome(PlacementImportDate.class); try { season = schoolBusiness.getCurrentSchoolSeason(schoolBusiness.getCategoryElementarySchool()); } catch (FinderException e) { e.printStackTrace(); println("NackaHighSchoolPlacementHandler: School season is not defined."); return false; } transaction.begin(); // iterate through the records and process them String item; int count = 0; boolean failed = false; while (!(item = (String) file.getNextRecord()).trim().equals("")) { count++; if (!processRecord(item, count)) { failedRecords.add(item); failed = true; // break; } if ((count % 100) == 0) { System.out.println( "NackaHighSchoolHandler processing RECORD [" + count + "] time: " + IWTimestamp.getTimestampRightNow().toString()); } item = null; } if (!failed) { if (!terminateOldPlacements()) { failed = true; } } printFailedRecords(); clock.stop(); println("Number of records handled: " + (count - 1)); println( "Time to handle records: " + clock.getTime() + " ms OR " + ((int) (clock.getTime() / 1000)) + " s"); // success commit changes if (!failed) { transaction.commit(); } else { transaction.rollback(); } report.store(false); return !failed; } catch (Exception e) { e.printStackTrace(); try { transaction.rollback(); } catch (SystemException e2) { e2.printStackTrace(); } report.store(false); return false; } }