/** * 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(); } }
/** 针对oracle Clob字段的插入操作 */ @Test public void testBigClobWrite() { PreparedDBUtil dbUtil = new PreparedDBUtil(); TransactionManager tm = new TransactionManager(); try { // 启动事务 tm.begin(); // 先插入一条记录,blob字段初始化为empty_lob dbUtil.preparedInsert("insert into test(id,clobname) values(?,?)"); String id = DBUtil.getNextStringPrimaryKey("test"); dbUtil.setString(1, id); dbUtil.setClob(2, CLOB.empty_lob()); // 先设置空的blob字段 dbUtil.executePrepared(); // 查找刚才的插入的记录,修改blob字段的值为一个文件 dbUtil = new PreparedDBUtil(); dbUtil.preparedSelect("select clobname from test where id = ?"); dbUtil.setString(1, id); dbUtil.executePrepared(); CLOB clob = (CLOB) dbUtil.getClob(0, "clobname"); if (clob != null) { DBUtil.updateCLOB(clob, new java.io.File("d:\\route.txt")); } tm.commit(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); try { tm.rollback(); } catch (RollbackException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } finally { tm = null; dbUtil = null; } }
@Test public void testScenario1() throws Exception { Context ctx = cache.getJNDIContext(); UserTransaction ta = null; // Connection conn = null; try { qs.createIndex("iIndex", IndexType.FUNCTIONAL, "ID", "/portfolios"); // PUT 4 objects in region, QUERY, CREATEINDEX for (int i = 0; i < 4; i++) { currRegion.put("key" + i, new Portfolio(i)); } QueryObserverImpl observer2 = new QueryObserverImpl(); QueryObserverHolder.setInstance(observer2); q = qs.newQuery("select distinct * from /portfolios where ID != 53"); Object r = q.execute(); if (!observer2.isIndexesUsed) { fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED"); } if (((Collection) r).size() != 4) { fail("Query result not of expected size"); } // print("Size of query result :"+ ((Collection)r).size()); // print("Result of query =" + Utils.printResult(r)); // print("Index IS: " + ((RangeIndex)i2).dump()); // BEGIN TX PUT new 4 objects in region, QUERY,CREATEINDEX, ROLLBACK ta = (UserTransaction) ctx.lookup("java:/UserTransaction"); ta.begin(); for (int i = 9; i < 13; i++) { currRegion.put("key" + i, new Portfolio(i)); } observer2 = new QueryObserverImpl(); QueryObserverHolder.setInstance(observer2); q = qs.newQuery("select distinct * from /portfolios where ID != 53"); r = q.execute(); if (!observer2.isIndexesUsed) { fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED"); } if (((Collection) r).size() != 4) { fail("Query result not of expected size"); } // print("Size of query result :"+ ((Collection)r).size()); // print("Result of query =" + Utils.printResult(r)); Index i1 = qs.createIndex("tIndex", IndexType.FUNCTIONAL, "status", "/portfolios"); // print("Index IS: " + ((RangeIndex)i1).dump()); observer2 = new QueryObserverImpl(); QueryObserverHolder.setInstance(observer2); q = qs.newQuery("select distinct * from /portfolios where status = 'active'"); r = q.execute(); if (!observer2.isIndexesUsed) { fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED"); } if (((Collection) r).size() != 2) { fail("Query result not of expected size"); } observer2 = new QueryObserverImpl(); QueryObserverHolder.setInstance(observer2); q = qs.newQuery("select distinct * from /portfolios where status = 'inactive'"); r = q.execute(); if (!observer2.isIndexesUsed) { fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED"); } if (((Collection) r).size() != 2) { fail("Query result not of expected size"); } ta.rollback(); // PRINT INDEX AFTER ROLLBACK, REMOVEINDEX. observer2 = new QueryObserverImpl(); QueryObserverHolder.setInstance(observer2); q = qs.newQuery("select distinct * from /portfolios where status = 'active'"); r = q.execute(); if (!observer2.isIndexesUsed) { fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED"); } if (((Collection) r).size() != 2) { fail("Query result not of expected size"); } // print("AfterRollback \n"+currRegion.values()); // print("Index IS: " + ((RangeIndex)i1).dump()); qs.removeIndex(i1); // BEGIN TX PUT new 4 objects in region,CREATEINDEX, QUERY ,COMMIT ta.begin(); for (int i = 9; i < 13; i++) { currRegion.put("key" + i, new Portfolio(i)); } i1 = qs.createIndex("tIndex", IndexType.FUNCTIONAL, "status", "/portfolios"); // print("Index IS: " + ((RangeIndex)i1).dump()); observer2 = new QueryObserverImpl(); QueryObserverHolder.setInstance(observer2); q = qs.newQuery("select distinct * from /portfolios where status = 'active'"); r = q.execute(); if (!observer2.isIndexesUsed) { fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED"); } if (((Collection) r).size() != 2) { fail("Query result not of expected size"); } for (int i = 9; i < 13; i++) { currRegion.put("key" + i, new Portfolio(i)); } observer2 = new QueryObserverImpl(); QueryObserverHolder.setInstance(observer2); q = qs.newQuery("select distinct * from /portfolios where status = 'active'"); r = q.execute(); if (!observer2.isIndexesUsed) { fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED"); } if (((Collection) r).size() != 2) { fail("Query result not of expected size"); } // print("Size of query result :"+ ((Collection)r).size()); // print("Result of query =" + Utils.printResult(r)); // print("Index on status IS: " + ((RangeIndex)i1).dump()); // print("Index On ID IS: " + ((RangeIndex)i2).dump()); ta.commit(); // WAIT FOR 2 secs DISPLAYINDEX, QUERY Thread.sleep(2000); // print("Aftercommit \n"+currRegion.values()); // print("Index IS: " + ((RangeIndex)i1).dump()); observer2 = new QueryObserverImpl(); QueryObserverHolder.setInstance(observer2); q = qs.newQuery("select distinct * from /portfolios where status = 'active'"); r = q.execute(); if (!observer2.isIndexesUsed) { fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED"); } if (((Collection) r).size() != 4) { fail("Query result not of expected size"); } observer2 = new QueryObserverImpl(); QueryObserverHolder.setInstance(observer2); q = qs.newQuery("select distinct * from /portfolios where status = 'inactive'"); r = q.execute(); if (!observer2.isIndexesUsed) { fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED"); } if (((Collection) r).size() != 4) { fail("Query result not of expected size"); } observer2 = new QueryObserverImpl(); QueryObserverHolder.setInstance(observer2); q = qs.newQuery("select distinct * from /portfolios where ID != 53"); r = q.execute(); if (!observer2.isIndexesUsed) { fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED"); } if (((Collection) r).size() != 8) { fail("Query result not of expected size"); } // print("Size of query result :"+ ((Collection)r).size()); // print("Result of query =" + Utils.printResult(r)); // print("Index On ID IS: " + ((RangeIndex)i2).dump()); } catch (RollbackException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); ta.rollback(); } }