当前位置: 首页 > news >正文

网站效果案例知名室内设计网站

网站效果案例,知名室内设计网站,衡水网站建设多少钱,it外包公司招聘由openSession、getCurrentSession和HibernateDaoSupport浅谈Spring对事物的支持 Spring和Hibernate的集成的一个要点就是对事务的支持#xff0c;openSession、getCurrentSession都是编程式事务#xff08;手动设置事务的提交、回滚#xff09;中重要的对象#xff0c;Hi… 由openSession、getCurrentSession和HibernateDaoSupport浅谈Spring对事物的支持     Spring和Hibernate的集成的一个要点就是对事务的支持openSession、getCurrentSession都是编程式事务手动设置事务的提交、回滚中重要的对象HibernateDaoSupport则提供了更方便的声明式事务支持。     Hibernate中最重要的就是Session对象的引入它是对jdbc的深度封装包括对事务的处理Session对象通过SessionFactory来管理openSession和getCurrentSession是管理session的重要的方法。     openSession和getCurrentSession的根本区别在于有没有绑定当前线程所以使用方法有差异 * openSession没有绑定当前线程所以使用完后必须关闭 * currentSession和当前线程绑定,在事务结束后会自动关闭。 关于事务的边界和传播      通常情况下事务的边界需要设置在业务逻辑处理层中但是如果在一个业务中涉及到多个业务逻辑层之间的方法且需要在同一个事务中运行那么这就涉及到了事务的传播性。 如果使用openSession就要在dao层的方法中传递session而这种做法是很糟糕的首先增加了参数的个数另外方法是否需要事务完全是可以当做一种独立的服务抽离出的。 因为currentSession是线程级别的所以只要业务逻辑方法在同一个线程中就不会担心上面的问题。这也是currentSession的一个优越处之一。 使用currentSession 1.在配置文件中将线程配置成Thread级别的。   [html] view plaincopyprint? SPAN styleFONT-SIZE: 18pxpropertynamepropertynamehibernate.current_session_context_classthread/property/SPAN   propertynamehibernate.current_session_context_classthread/property 2.调用sessionFactory的getCurrentSession方法   [java] view plaincopyprint? SPAN styleFONT-SIZE: 18pxpublicvoid addUser(User user) {        Session session  null;        try {           session HibernateUtils.getSessionFactory().getCurrentSession();           session.beginTransaction();                 session.save(user);                   Loglog  new Log();           log.setType(操作日志);           log.setTime(new Date());           log.setDetail(XXX);                 LogManager logManager  newLogManagerImpl();           logManager.addLog(log);                  session.getTransaction().commit();        }catch(Exception e) {           e.printStackTrace();           session.getTransaction().rollback();           }    }/SPAN   publicvoid addUser(User user) {Session session null;try {session HibernateUtils.getSessionFactory().getCurrentSession();session.beginTransaction(); session.save(user); Loglog new Log();log.setType(操作日志);log.setTime(new Date());log.setDetail(XXX); LogManager logManager newLogManagerImpl();logManager.addLog(log); session.getTransaction().commit();}catch(Exception e) {e.printStackTrace();session.getTransaction().rollback(); }}   使用openSession:   [java] view plaincopyprint? SPAN styleFONT-SIZE: 18pxpublic void addUser(User user) {          Sessionsession  null;          try{             session HibernateUtils.getSession();             session.beginTransaction();    // 若干操作…………                      session.getTransaction().commit();          }catch(Exceptione) {             e.printStackTrace();             session.getTransaction().rollback();          }finally{             HibernateUtils.closeSession(session);          }       }     /SPAN   public void addUser(User user) {Sessionsession null;try{session HibernateUtils.getSession();session.beginTransaction();// 若干操作………… session.getTransaction().commit();}catch(Exceptione) {e.printStackTrace();session.getTransaction().rollback();}finally{HibernateUtils.closeSession(session);}} 使用HibernateDaoSupport声明式事务     Spring与Hibernate的集成使用最多的是HibernateDaoSupport它对session的获取以及事务做了进一步的封装,只需要关注dao的实现,而不用担心某个地方的事务是否关闭。   [java] view plaincopyprint? SPAN styleFONT-SIZE: 18pxthis.getHibernateTemplate().save(user);/SPAN   this.getHibernateTemplate().save(user);   关于异常与事务回滚:        Spring在遇到运行期异常(继承了RuntimeException)的时候才会回滚如果是Exception(如用户输入密码错误)抛出就好事务会继续往下进行。     Spring对异常的处理的灵活性还是比较高的,可以配置遇到某个Exception进行回滚某个RuntimeException不回滚,但是对于EJB就没有这么灵活了,EJB相当于是固定的套餐。 不会回滚:     [java] view plaincopyprint? public void addUser(User user)       throws Exception {          this.getHibernateTemplate().save(user);             //若干操作……                     throw new Exception();       }   public void addUser(User user)throws Exception {this.getHibernateTemplate().save(user);//若干操作…… throw new Exception();}   回滚:   [java] view plaincopyprint? public void addUser(User user) {           this.getHibernateTemplate().save(user);               //若干操作……                   throw new RuntimeException();     }   public void addUser(User user) {this.getHibernateTemplate().save(user); //若干操作…… throw new RuntimeException();}     Spring与Hibernate的集成,使用HibernateDaoSupport的配置:    在ssh框架应用中Spring与Hibernate的事务集成基本上是比较固定的我们把事务的集成单独配置到applicationContext-common.xml中   [html] view plaincopyprint? SPAN styleFONT-SIZE: 18px?xml version1.0encodingUTF-8?     beansxmlnsbeansxmlnshttp://www.springframework.org/schema/beans           xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance           xmlns:aophttp://www.springframework.org/schema/aop           xmlns:txhttp://www.springframework.org/schema/tx            xsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd              http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd               http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd       !--配置SessionFactory --       beanidbeanidsessionFactoryclassorg.springframework.orm.hibernate3.LocalSessionFactoryBean          propertynamepropertynameconfigLocation             valueclasspath:hibernate.cfg.xml/value          /property       /bean           !--配置事务管理器 --       beanidbeanidtransactionManagerclassorg.springframework.orm.hibernate3.HibernateTransactionManager          propertynamepropertynamesessionFactory             refbeanrefbeansessionFactory/                  /property       /bean           !--那些类那些方法使用事务 --       aop:config          aop:pointcutidaop:pointcutidallManagerMethod expressionexecution(*com.bjpowernode.usermgr.manager.*.*(..))/          aop:advisorpointcut-refaop:advisorpointcut-refallManagerMethod advice-reftxAdvice/       /aop:config           !--事务的传播特性 --        tx:adviceidtx:adviceidtxAdvice transaction-managertransactionManager          tx:attributes             tx:methodnametx:methodnameadd* propagationREQUIRED/             tx:methodnametx:methodnamedel* propagationREQUIRED/             tx:methodnametx:methodnamemodify* propagationREQUIRED/             tx:methodnametx:methodname* propagationREQUIREDread-onlytrue/          /tx:attributes       /tx:advice    /beans/SPAN   ?xml version1.0encodingUTF-8? beansxmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:aophttp://www.springframework.org/schema/aopxmlns:txhttp://www.springframework.org/schema/txxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd!--配置SessionFactory --beanidsessionFactoryclassorg.springframework.orm.hibernate3.LocalSessionFactoryBeanpropertynameconfigLocationvalueclasspath:hibernate.cfg.xml/value/property/bean!--配置事务管理器 --beanidtransactionManagerclassorg.springframework.orm.hibernate3.HibernateTransactionManagerpropertynamesessionFactoryrefbeansessionFactory/ /property/bean!--那些类那些方法使用事务 --aop:configaop:pointcutidallManagerMethod expressionexecution(*com.bjpowernode.usermgr.manager.*.*(..))/aop:advisorpointcut-refallManagerMethod advice-reftxAdvice//aop:config!--事务的传播特性 -- tx:adviceidtxAdvice transaction-managertransactionManagertx:attributestx:methodnameadd* propagationREQUIRED/tx:methodnamedel* propagationREQUIRED/tx:methodnamemodify* propagationREQUIRED/tx:methodname* propagationREQUIREDread-onlytrue//tx:attributes/tx:advice/beans     因为在hibernate.cfg.xml中添加了如下配置所以在tomcat等容器启动的时候会自动将相应的bean对象创建。   [html] view plaincopyprint? SPAN styleFONT-SIZE: 18px propertynamepropertynamehibernate.hbm2ddl.autoupdate/property/SPAN   propertynamehibernate.hbm2ddl.autoupdate/property   applicationContext-beans.xml     通常将业务逻辑对实现类的引用单独的xml文件中同时在实现类中不能忽略sessionFactory工厂的注入。   [html] view plaincopyprint? SPAN styleFONT-SIZE: 18px?xml version1.0encodingUTF-8?     beansxmlnsbeansxmlnshttp://www.springframework.org/schema/beans           xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance            xmlns:aophttp://www.springframework.org/schema/aop           xmlns:txhttp://www.springframework.org/schema/tx           xsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd               http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd              http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd                     beanidbeaniduserManager classcom.bjpowernode.usermgr.manager.UserManagerImpl          propertynamepropertynamesessionFactory refsessionFactory/          propertynamepropertynamelogManager reflogManager/       /bean           beanidbeanidlogManagerclasscom.bjpowernode.usermgr.manager.LogManagerImpl          propertynamepropertynamesessionFactory refsessionFactory/       /bean    /beans/SPAN   ?xml version1.0encodingUTF-8? beansxmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:aophttp://www.springframework.org/schema/aopxmlns:txhttp://www.springframework.org/schema/txxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsdbeaniduserManager classcom.bjpowernode.usermgr.manager.UserManagerImplpropertynamesessionFactory refsessionFactory/propertynamelogManager reflogManager//beanbeanidlogManagerclasscom.bjpowernode.usermgr.manager.LogManagerImplpropertynamesessionFactory refsessionFactory//bean/beans 事务传播特性    为了保证调用的业务逻辑方法都使用同一个事务通常都使用REQUIRED这个级别它表示如果上一个方法中有事务就直接使用如果没有就创建一个事务这样一旦事务创建了后后续调用的方法就不会再创建。    其他的事务传播特性见下表   Spring事务的隔离级别    1. ISOLATION_DEFAULT 这是一个PlatfromTransactionManager默认的隔离级别使用数据库默认的事务隔离级别。         另外四个与JDBC的隔离级别相对应。    2. ISOLATION_READ_UNCOMMITTED 这是事务最低的隔离级别它充许令外一个事务可以看到这个事务未提交的数据。         这种隔离级别会产生脏读不可重复读和幻像读。    3. ISOLATION_READ_COMMITTED 保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据    4. ISOLATION_REPEATABLE_READ 这种事务隔离级别可以防止脏读不可重复读。但是可能出现幻像读。         它除了保证一个事务不能读取另一个事务未提交的数据外还保证了避免下面的情况产生(不可重复读)。    5. ISOLATION_SERIALIZABLE 这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。      除了防止脏读不可重复读外还避免了幻像读。     事务隔离级别主要应用在对大数据的处理方面与锁的机制是密不可分的这里不赘述。 转载于:https://www.cnblogs.com/gtaxmjld/p/4819725.html
http://www.zqtcl.cn/news/359568/

相关文章:

  • 英文网站seo发展前景成都网站开发建设推广
  • p2p网贷网站建设方案制作网站专业公司吗
  • 益阳网站建设企业房地产市场最新动态
  • 关于公司网站改版通知jmr119色带
  • 城关区建设局网站珠海中英文网站建设
  • 长春哪家做网站便宜手机英语网站
  • 应城网站建设莱芜拉呱
  • 如何建立淘宝客网站HTML网站建设课程
  • 网站建设供需chrome不安全的网站设置
  • 网站dns修改中国楼市未来发展趋势
  • 网站超级链接怎么做帮别人发广告赚钱平台
  • 做网站可以赚钱么注册做网站的公司
  • 河南省建协网官方网站建网站卖阀门
  • 医院网站怎么制作重庆安全监督工程信息网
  • 饰品网站建设规划书搭建微信网站
  • 开发网站访问流量赚钱加盟网站需要怎么做
  • 装饰协会网站源码湖南省郴州市北湖区
  • 花都网站建设价格重庆市住房和城乡建设厅网站
  • 北京住总第一开发建设有限公司网站wordpress 网站访问认证页面
  • 网站制作的管理苏州百度推广服务中心
  • 厦门建行网站首页企业展厅建筑外观
  • 重庆定制型网站建设1000套网站源码
  • 阿里云网站建设服务费会计科目安平县建设局网站
  • 网上做国外兼职网站网络编程技术实验报告
  • iis网站服务器安全隐患分析创新的合肥网站建设
  • 蛋糕网站建设方案广州网站公司推荐
  • 无锡seo公司网站广渠门做网站的公司
  • 安徽股票配资网站建设seo教程自学网
  • 网站建设酷隆做3d建模贴图找哪个网站
  • 天津市工程建设交易管理中心网站自己如何搭建服务器