/**
   * 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;
  }
Ejemplo n.º 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;
 }
Ejemplo n.º 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;
 }
Ejemplo n.º 5
0
 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;
 }
Ejemplo n.º 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();
 }
Ejemplo n.º 9
0
 public static Session getSession() {
   return sessionFactory.openSession();
 }
Ejemplo n.º 10
0
 public static Session getCurrentSession() {
   return sessionFactory.getCurrentSession();
 }