Package org.hibernate

Examples of org.hibernate.Transaction


   */
  public void beginTransaction(){
    if(transactions == null)
      return;
    // Would be written as a no-op in an EJB container with CMT
    Transaction tx = (Transaction)transactions.get();
    if (tx == null || tx.wasCommitted() || tx.wasRolledBack()) {
      Session ssn = (Session)sessions.get();
      if(ssn == null){
        ssn = getSession();
        tx = ssn.beginTransaction();
        transactions.set(tx);
View Full Code Here


   */
  public void commit(){
    if(transactions == null)
      return;
    // Would be written as a no-op in an EJB container with CMT
    Transaction tx = (Transaction)transactions.get();
    if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
      tx.commit();
    }
    else{
      if(tx!=null && log.isWarnEnabled())
        log.warn("Trying to commit the uncommitable transaction, nothing to do.");
    }
View Full Code Here

   * Rollback the database transaction.
   */
  public void rollback(){
    if(transactions == null)
      return;
    Transaction tx = (Transaction)transactions.get();
    transactions.set(null);
    if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
      tx.rollback();
    }
    else{
      if(tx!=null && log.isWarnEnabled())
        log.warn("Trying to rollback the unrollbackable transaction, nothing to do.");
    }
View Full Code Here

    xml = Main.class.getResource("new_hibernate.cfg.xml");
    new_hb = Hibernate.init(xml.getPath());

    Session old_ssn = old_hb.getSession();
    Session new_ssn = new_hb.getSession();
    Transaction tx = new_ssn.beginTransaction();
   
    try{
      upgradeUsers(old_ssn, new_ssn);
      System.out.println("============== Users upgraded.=================");
     
      upgradeLinks(old_ssn, new_ssn);
      System.out.println("============== Links upgraded.=================");
     
      upgradeCatalogs(old_ssn, new_ssn);
      System.out.println("============== Catalogs upgraded.=================");
     
      upgradeLogs(old_ssn, new_ssn);
      System.out.println("============== Articles upgraded.=================");
     
      upgradeReplies(old_ssn, new_ssn);
      System.out.println("============== Replies upgraded.=================");

      upgradeBookmarks(old_ssn, new_ssn);
      System.out.println("============== Bookmarks upgraded.=================");
     
      upgradeMessages(old_ssn, new_ssn);
      System.out.println("============== Messages upgraded.=================");
     
      tx.commit();
      System.out.println("============== DLOG4J upgraded.=================");
    }catch(Exception e){
      e.printStackTrace();
      tx.rollback();
    }finally{
      old_hb.destroy();
      new_hb.destroy();
    }
       
View Full Code Here

  @Override
  public Object execute(Method method, Object[] parameters) throws Throwable {
    if (!withoutService)
      return action.execute(method, parameters);
    else {
      Transaction tx = session.beginTransaction();
      try {
        Object result = action.execute(method, parameters);
        tx.commit();
        return result;
      } catch (Exception e) {
        tx.rollback();
        throw e.getCause();
      }
    }
  }
View Full Code Here

      transactional = method.getDeclaringClass().getAnnotation(Transactional.class);
    }

    TransactionalType type = transactional.type();
   
    final Transaction transaction = session.getSession().getTransaction();

    if(type != TransactionalType.READOLNY){
      session.setNeed2ProcessTransaction(true);
    }
   
    if(transaction.isActive()){
      return methodInvocation.proceed();
    }

    //开始一个新的事务
    if(type != TransactionalType.READOLNY){
      transaction.begin();
    }
   
    Object result = null;
    try {
      //执行被拦截的业务方法
      result = methodInvocation.proceed();
      //提交事务
      if(type != TransactionalType.READOLNY){
        transaction.commit();
      }
    } catch (Exception e) {
      //回滚当前事务
      if(type != TransactionalType.READOLNY && transaction.isActive()){
        transaction.rollback();
      }
      throw e;
    }
   
    log.debug("[HibernateLocalTransactionInterceptor]离开=》"+methodInvocation.getMethod().getName());
View Full Code Here

  @Override
  public String intercept(ActionInvocation invocation) throws Exception {
    boolean need2ProcessTransaction = false;
   
    String result = null;
    Transaction hbTS = null;
    EntityTransaction jpaTS = null;
   
    try {
      result = invocation.invoke();

      if(PersistenceGuiceContext.getInstance().isUseJPA()){
        EntityManagerFactoryHolder emfH = GuiceContext.getInstance().getBean(EntityManagerFactoryHolder.class);
        EntityManagerInfo entityManager = emfH.getEntityManagerInfo();
        jpaTS = entityManager.getEntityManager().getTransaction();
        need2ProcessTransaction = entityManager.isNeed2ProcessTransaction();
      }else if(PersistenceGuiceContext.getInstance().isUseHibernate()){
        SessionFactoryHolder sessionFH = GuiceContext.getInstance().getBean(SessionFactoryHolder.class);
        SessionInfo session = sessionFH.getSessionInfo();
        hbTS = session.getSession().getTransaction();
        need2ProcessTransaction = session.isNeed2ProcessTransaction() && !hbTS.wasCommitted();
      }

      if(need2ProcessTransaction){
        if(hbTS != null)
          hbTS.commit();
        if(jpaTS != null)
          jpaTS.commit();
      }
    } catch (Exception e) {
      if(hbTS != null)
        hbTS.rollback();
      if(jpaTS != null)
        jpaTS.rollback();
      throw e;
    }
    return result;
View Full Code Here

  public void testWriteHibernate() throws Exception {

    Session session = HibernateHelper.currentSession();

    Transaction tx = session.beginTransaction();

    Test princess = new Test();
    princess.setName("Princess");
    princess.setFirstname("Fiona");
    princess.setId(new Integer(11));
    princess.setZip(new Integer(00001));
    princess.setName("shrek");

    session.save(princess);
    tx.commit();
    session.getTransaction().commit();

    HibernateHelper.closeSession();

    System.out.println("testWriteHibernate Done");
View Full Code Here

  public void testUnreadItems() throws Exception {
    ChannelBuilder builder = new ChannelBuilder(session);
    int chId = -1;
    String chanName = "Unread Tester";
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      ChannelIF channel = builder.createChannel(chanName);
      channel.setDescription("Test Channel: " + chanName);
      session.save(channel);
      // Add items, with some marked read and some marked unread
      int items;
      for (items = 0; items < 20; items++) {
        boolean unreadflag = ((items > 0 && items < 5))
          || (items > 10 && items < 15);
        String desc = unreadflag ? "UnreadItem" : "ReadItem";
        ItemIF anItem = builder.createItem(channel, "Item: " + items, desc, new URL("http://www.sf.net"));
        anItem.setUnRead(unreadflag);
      }
      session.save(channel);
      chId = (int) channel.getId();
      tx.commit();
    }
    catch (HibernateException he) {
      logger.warn("trying to rollback the transaction");
      if (tx != null) tx.rollback();
      throw he;
    }
    assertTrue("No valid channel created.", chId >= 0);

    // -- try to retrieve channel and check the unread statuses
View Full Code Here

   
    // --- create a new cat
    CategoryIF catA = builder.createCategory(null, "News Category");
    Long catId = null;
   
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      session.save(catA);
      tx.commit();
      catId = new Long(catA.getId());
      System.out.println("Saved category with id " + catId + " persistently");
    }
    catch (HibernateException he) {
      if (tx != null) tx.rollback();
      throw he;
    }
    finally {
      session.close();
    }
   
    // --- update
    session = handler.getSession();
    try {
      tx = session.beginTransaction();
      Category theCat = (Category) session.load(Category.class, catId);
      theCat.setTitle("Another category title");
      tx.commit();
      System.out.println("Updated category title for id: " + catId);
    } catch (HibernateException he) {
      if (tx != null) tx.rollback();
      throw he;
    }
    finally {
      session.close()
    }
   
    // --- list
    session = handler.getSession();
    try {
      tx = session.beginTransaction();
      // Query q = session.createQuery("select cat.id from Category as cat");
      // List result = q.list();
      Query q = session.createQuery("from Category as cat where cat.title = :title");
      q.setParameter("title", "Another category title", Hibernate.STRING);
      List cats = q.list();
      tx.commit();
      Iterator it = cats.iterator();
      while (it.hasNext()) {
        Category c = (Category) it.next();
        System.out.println("--> " + c.getId());
      }
    } catch (HibernateException he2) {
      if (tx != null) tx.rollback();
      throw he2;
    }
    finally {
      session.close()
    }
View Full Code Here

TOP

Related Classes of org.hibernate.Transaction

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.