/**
   * Gets Hibernate session.
   *
   * @param tx Cache transaction.
   * @return Session.
   */
  Session session(@Nullable GridCacheTx tx) {
    Session ses;

    if (tx != null) {
      ses = tx.meta(ATTR_SES);

      if (ses == null) {
        ses = sesFactory.openSession();

        ses.beginTransaction();

        // Store session in transaction metadata, so it can be accessed
        // for other operations on the same transaction.
        tx.addMeta(ATTR_SES, ses);

        if (log.isDebugEnabled())
          log.debug("Hibernate session open [ses=" + ses + ", tx=" + tx.xid() + "]");
      }
    } else {
      ses = sesFactory.openSession();

      ses.beginTransaction();
    }

    return ses;
  }
Example #2
0
 public static void closeFactory() {
   if (sessionFactory != null) {
     try {
       sessionFactory.close();
     } catch (HibernateException e) {
       System.out.println(e);
     }
   }
 }
 public static Session currentSession() {
   Session s = (Session) session.get();
   // Open a new Session, if this Thread has none yet
   if (s == null) {
     s = sessionFactory.openSession();
     session.set(s);
   }
   return s;
 }
Example #4
0
 public static Session openSession() throws HibernateException {
   buildIfNeeded();
   Session session = (Session) threadLocal.get();
   if (session == null || !session.isOpen()) {
     session = sessionFactory.openSession();
     threadLocal.set(session);
   }
   return session;
 }
 public static Session currentSession() throws HibernateException {
   Session s = session.get();
   // 如果该线程还没有Session,则创建一个新的Session
   if (s == null) {
     s = sessionFactory.openSession();
     // 将获得的Session变量存储在ThreadLocal变量session里
     session.set(s);
   }
   return s;
 }
Example #6
0
  public static void main(String[] args) {
    Configuration cfg = new Configuration();
    cfg = cfg.configure("hibernate.oracle.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();
    Session ses = factory.openSession();

    Bank b = new Bank();
    String name;
    name = "SBI";
    // b.setSid(sid);
    b.setName(name);
    Transaction tx = ses.beginTransaction();
    ses.persist(b);
    tx.commit();
    ses.close();

    System.out.println("Record Inserted!");
    factory.close();
  }
  /** {@inheritDoc} */
  @Override
  public void onSessionStart(CacheStoreSession ses) {
    if (ses.attachment() == null) {
      try {
        Session hibSes = sesFactory.openSession();

        ses.attach(hibSes);

        if (ses.isWithinTransaction()) hibSes.beginTransaction();
      } catch (HibernateException e) {
        throw new CacheWriterException(
            "Failed to start store session [tx=" + ses.transaction() + ']', e);
      }
    }
  }
 /** {@inheritDoc} */
 @Override
 public void stop() throws IgniteException {
   if (closeSesOnStop && sesFactory != null && !sesFactory.isClosed()) sesFactory.close();
 }
 public static Session getSession() {
   return sessionFactory.openSession();
 }
Example #10
0
 public static Session getCurrentSession() {
   return sessionFactory.getCurrentSession();
 }