读书人

Spring自个儿控制事务

发布时间: 2012-06-28 15:20:03 作者: rapoo

Spring自己控制事务
applicationContext.xml

<bean id="userInfoDao" name="code">public class TestOgi1DaoImpl extends HibernateDaoSupport implements TestOgi1Dao{  public boolean save(TestOgi1 transientInstance) {          log.debug("saving TestOgi1 instance");          Session session = null;          Transaction tx = null;          try {              // System.out.println(this.getHibernateTemplate().getSessionFactory().getCurrentSession());              // getHibernateTemplate().merge(transientInstance);              session = this.getSession(true);                tx = session.beginTransaction();                session.save(transientInstance);              tx.commit();              log.debug("save successful");              return true;          } catch (RuntimeException re) {              log.error("save failed", re);              tx.rollback();              throw re;          } finally {                            if (session.isOpen()) {                  session.close();              }          }      }    public List findByProperty(String propertyName, Object value) {          log.debug("finding TestOgi1 instance with property: " + propertyName                  + ", value: " + value);          try {              String queryString = "from TestOgi1 as model where model."                      + propertyName + "= ?";              return getHibernateTemplate().find(queryString, value);          } catch (RuntimeException re) {              log.error("find by property name failed", re);              throw re;          }      }  }

读书人网 >开源软件

热点推荐