??xml version="1.0" encoding="utf-8" standalone="yes"?>国产亚洲精品国看不卡,狠狠色伊人亚洲综合成人,久久精品亚洲福利http://www.tkk7.com/cherishchen/category/23924.htmlThe art of living is to know when to hold fast and when to let gozh-cnFri, 24 Aug 2007 22:34:37 GMTFri, 24 Aug 2007 22:34:37 GMT60也来写个struts2 CURD的例?Move CRUD Operations into the same Actionhttp://www.tkk7.com/cherishchen/archive/2007/08/23/138760.html凭栏观v凭栏观vThu, 23 Aug 2007 03:02:00 GMThttp://www.tkk7.com/cherishchen/archive/2007/08/23/138760.htmlhttp://www.tkk7.com/cherishchen/comments/138760.htmlhttp://www.tkk7.com/cherishchen/archive/2007/08/23/138760.html#Feedback0http://www.tkk7.com/cherishchen/comments/commentRss/138760.htmlhttp://www.tkk7.com/cherishchen/services/trackbacks/138760.html阅读全文

凭栏观v 2007-08-23 11:02 发表评论
]]>
JPA --Java EE 5.0 ORM 规范Q{Q?/title><link>http://www.tkk7.com/cherishchen/archive/2007/07/21/131658.html</link><dc:creator>凭栏观v</dc:creator><author>凭栏观v</author><pubDate>Sat, 21 Jul 2007 12:50:00 GMT</pubDate><guid>http://www.tkk7.com/cherishchen/archive/2007/07/21/131658.html</guid><wfw:comment>http://www.tkk7.com/cherishchen/comments/131658.html</wfw:comment><comments>http://www.tkk7.com/cherishchen/archive/2007/07/21/131658.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/cherishchen/comments/commentRss/131658.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/cherishchen/services/trackbacks/131658.html</trackback:ping><description><![CDATA[<p>作? 它山 出处:<a >IT168</a></p> <p><a title="http://tech.it168.com/j/n/2007-03-05/200703051354348.shtml" >http://tech.it168.com/j/n/2007-03-05/200703051354348.shtml</a></p> <p>PA作ؓ一对象持久化的标准,不但可以获得Java EE应用服务器的支持Q还可以直接在Java SE中用?/p> <p>介绍得很清楚的文?/p> <p> </p> <p><strong>JPA概述</strong> <p>JPA(Java Persistence API)作ؓJava EE 5.0q_标准的ORM规范Q将得到所有Java EE服务器的支持。Sunq次吸取了之前EJB规范惨痛p|的经历,在充分吸收现有ORM框架的基上,得到了一个易于用、׾~性强的ORM规范。从目前的开发社区的反应上看QJPA受到了极大的支持和赞扬,JPA作ؓORM领域标准化整合者的目标应该不难实现?nbsp; <p>JPA通过JDK 5.0注解或XML描述对象Q关p表的映关p,q将q行期的实体对象持久化到数据库中Q图 1很好地描qCJPA的结构: <p><img height="305" alt="" src="http://developer.51cto.com/files/uploadimg/20070305/1547200.JPG" width="450"> <p>Sun引入新的JPA ORM规范Z两个原因Q其一Q简化现有Java EE和Java SE应用的对象持久化的开发工作;其二QSun希望整合对ORM技术,实现天下归一?nbsp; <p>JPA由EJB 3.0软g专家l开发,作ؓJSR-220实现的一部分。但它不囿于EJB 3.0Q你可以在Web应用、甚x面应用中使用。JPA的宗旨是为POJO提供持久化标准规范,由此可见Q经q这几年的实跉|索,能够q容器独立q行Q方便开发和试的理念已l深入h心了。目前Hibernate 3.2、TopLink 10.1.3以及OpenJpa都提供了JPA的实现? <p>JPA的M思想和现有Hibernate、TopLinkQJDO{ORM框架大体一致。ȝ来说QJPA包括以下3斚w的技术:  <ul> <li>ORM映射元数据,JPA支持XML和JDK 5.0注解两种元数据的形式Q元数据描述对象和表之间的映关p,框架据此实体对象持久化到数据库表中Q?nbsp; <li>JPA 的APIQ用来操作实体对象,执行CRUD操作Q框架在后台替我们完成所有的事情Q开发者从J琐的JDBC和SQL代码中解脱出来?nbsp; <li>查询语言Q这是持久化操作中很重要的一个方面,通过面向对象而非面向数据库的查询语言查询数据Q避免程序的SQL语句紧密耦合?nbsp;</li></ul><img src ="http://www.tkk7.com/cherishchen/aggbug/131658.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/cherishchen/" target="_blank">凭栏观v</a> 2007-07-21 20:50 <a href="http://www.tkk7.com/cherishchen/archive/2007/07/21/131658.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JPAQ好与坏Q常见问题解{?/title><link>http://www.tkk7.com/cherishchen/archive/2007/07/19/131257.html</link><dc:creator>凭栏观v</dc:creator><author>凭栏观v</author><pubDate>Thu, 19 Jul 2007 06:14:00 GMT</pubDate><guid>http://www.tkk7.com/cherishchen/archive/2007/07/19/131257.html</guid><wfw:comment>http://www.tkk7.com/cherishchen/comments/131257.html</wfw:comment><comments>http://www.tkk7.com/cherishchen/archive/2007/07/19/131257.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/cherishchen/comments/commentRss/131257.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/cherishchen/services/trackbacks/131257.html</trackback:ping><description><![CDATA[<p><a target="_blank">JPAQ好与坏Q常见问题解{?/a></p> <p> </p> <p>问题QEJB专家团队是如何摆׃务描q符?<br>  回答Q在会话bean和消息驱动bean中,可以通过描述W和注释来控制事务的行ؓ。此外,我们默认的事务属性更改ؓ“REQUIRED”,q个默认值比以前的值“SUPPORTS”更常用。因此,完全不必Z务方法配|事务行为?br>  JPA实体仅供本地使用Q重点关注域模型。因此,无法在JPA实体上配|事务?或远E边界或安全?。而是必须使用会话bean façade(或消息驱动bean)Q才可以通过EJB协议使用q些实体。通常来说Q这是一件好事,配置安全性、远E处理和事务的粒度应该比持久化数据的_度_很多。JPA着重关注持久化数据Q以及与EJB的其他部分和Java EE规范集成h照管其他企业x炏V?br>  问题Q推荐对主键使用“long”还是“Long?如果允许使用null作ؓ|会如何?<br>  回答Q这实际上取决于您的数据模型。如果您的数据模型允怸键ؓnullQ那么用LongQ如果您的数据模型规定主键列不能为nullQ则使用 long更合适。ȝ来说Q我认ؓ对于非复合主键,允许null作ؓ合法值容易生؜淆,因此我們֐于用longQ而不是Long?br>  问题Q您说EJB 2.0不支持承,但是可以在几个不同位|?q程/bean)使用l承Q只是不在本C用而已。请解释一下?br>  回答Q根据EJB 2.1规范的附录D3Q?br>  当前的EJB规范未指定组件承的概念?br>  另一斚wQJPA规范实规定了实体承的概念。我们已l处理了EJB 2.1规范中指出的各种问题和复杂性,现在允许完全的多态查询和兌?br>  问题QBEA计划什么时候支?发布EJB3?<br>  WebLogic Server 10 Technology Preview 是完全符合规范的Java EE 5应用服务器。它包括完整的EJB3支持。WebLogic Server 10大概于三月下旬发布?br>  此外QKodo 是完全符合规范的生񔞮qAJPA实现Qƈ且已l发布?br>  问题QJPA是否支持l合主键?<br>  回答QJPA支持自然ID和组合IDQ以及数据库指派或实现指z数字倹{?br>  问题Q是否存在Spring模板Q像JDBC模板一样可以在容器外部使用?<br>  回答Q是的,Spring 2有JPA模板。但是,Spring 2可以对Q何标记着@Repository的bean执行JPA异常转译。因此,ȝ来说Q对于新的应用程序,最好直接用JPA APIQ而不是另一个模板层。对于用模板和正在q移到JPA的现有应用程序来_使用模板Ҏ比较合理?br>  此外Q可以像在Java EE服务器中一样将JPA的持久化单元部v到SpringQSpring对JPA规范中指出的EntityManager注入和查找服从容器规则?br>  问题QJPA是否支持JDK1.4?<br>  回答QJPA需要Java 5或更新版本?br>问题Q用范围查询时Q它是否也会q回l果L(例如Q返?38结果中?-10??<br>  回答Q不Q要惌得LQ必d出另外一个查询。通用模式是,在第一ơ执行搜索时获得LQ然后通过面览l果Q将L存储到方便的位置(会话状态、cookie{?Q?pre>  if (isFirstPage()) { // this is the first time we're executing this query <br>  Query q = em.createQuery("SELECT COUNT(p) FROM Product p WHERE ..."); <br>  long count = ((Long) q.getSingleResult()).longValue(); <br>  // store count somewhere stateful <br>  } <br>  Query q = em.createQuery("SELECT p FROM Product p WHERE ..."); <br>  q.setFirstResult(page * PAGE_SIZE); // page is stored somewhere stateful <br>  q.setMaxResults(PAGE_SIZE); </pre><br>  问题Q具有JPA包装器的Hibernate是不是一UEJB3实现?<br>  回答QJPA规范是完整的EJB3规范的子集,因此JPA实现本n不是完整的EJB3实现。我不了解RedHat的EJB3实现的情况如何。但QHibernate是JPA实现?br>  问题Q与Hibernate相比QJPA是不是更?<br>  回答QJPA是规范,而Hibernate是实现。因此,q是不同事物的比较。可以肯定,使用标准API比用专有API有更多优势,但不存在真正的劣ѝ?br>  问题Q是不是不再需要学习和使用Hibernate?<br>  回答Q规范团队关于JPA 1的目标之一是制定一个可以由很多供应商实现的APIQƈ且开发h员可以编码来实现该APIQ而不是用私有供应商Ҏ的API。我们已成功实现q个目标Q因此您只需使用供应商特有的API来获得JPA规范没有解决但您的应用程序中需要的功能。我的徏议是可能地使用JPA APIQ但是当需要供应商公开但是规范中没有提供的功能Ӟ则用供应商Ҏ的API?br>  例如QOpenJPA提供了保存点功能Q但JPA规范没有。因此,希望使用保存点的OpenJPA开发h员应该对代码的大部分内容使用JPA规范Q而借助OpenJPAEntityManager来设|和理保存炏V?br>  问题Q规范是否解决了~存问题?<br>  回答QJPA规范没有解决二~存问题(EntityManagerFactory-U?Q但是提供了实现该缓存必遵守的一些数据锁定和一致性规则,即在启用缓存时也是如此?br>  有少量与~存有关的主题可能会在将来的JPA规范版本中解冻I但是大多数缓存主题不必指定规则,q样Q不同的供应商就可以L地完成不同的工作。此处增加的最重要的内Ҏ一些基本缓存控制APIQ如回收某些对象IDQ或一些经常访问的ID固定到缓存中?br>  问题Q既然实体管理器承担了所有繁重的工作负蝲Q那么会话beanq有什么h?<br>  回答QEntityManager负责域对象模型和数据库之间的交互Q但是仍然在会话中实现安全性、事务控制、远E处理、有状态的临时数据存储Q而操作单元编E模型无法解决以上问题。会话beanq是部v单元和公用服务边界。因此,会话bean是定义所有业务代码的地方。换而言之,会话bean是EJB 容器x的,而JPA实现是在会话bean中用的?br>  当然Q您q可以直接从servlet或JSP或其他Q何可以用Java 5的地方用JPA。但是这L话,您就必须理自己的事务、处理自q集群服务故障转移、管理自q服务重部|等?br>问题Q相对于EJB2来说QEJB3可以处理多少个ƈ发事?<br>  回答Q从U会话bean的观ҎԌ臛_在WebLogic Server中,q发事务的数目没有什么差别。也是Q如果将您的应用E序从EJB2会话bean转换到EJB3会话beanQ但是完全没有修Ҏ久化机制Q可能不会发现重大差别。这是因为EJB3规范对会话bean部分的大多数更改着重实现编E模型的改进?br>  从实体bean的观ҎԌ我认为对于大多数应用E序QWebLogic Server的EJB 2.1和JPA支持的ƈ发事务数目相同。您可能发现JPA对于非主键的查询来说Q可伸羃性更高。一旦开始钻研Kodo?锁定l之cȝ功能Q则对于固定的域模型Q可以从ZJPA的系l中获得更多q发事务?br>  问题Q如何ؓAquaLogic DSP应用JPA?<br>  回答QAquaLogic DSP着重关注对数据的多重存储访问,q将数据作ؓ数据服务提供Q通常作ؓXML或SDO呈现q些数据。JPA规范着重关注与数据存储交互的Java API。可以设惻IJPAl定到AquaLogic DSPQ或SDOl定到Kodo产品(BEA的JPA实现)?br>  问题QJPA是否支持惰性加?<br>  回答Q是的。默认情况下QCollection和Mapcd的字D|惰性检索的Q而其他所有字D都是主动获取的。通过在字D늚持久化注解中指明“fetch”属性,可以Z各个字段静态地控制该行为?br>  问题Q什么是实现q程的最佳位|,例如Q检查许多用户及其帐?在银行应用程序中)以付l利?是在数据库的存储q程中实玎ͼq是在EJB中用JPA实现Q还是同时用这两种方式?<br>  回答Q根据我的经验,q实际上取决于组l因素,而不是其他因素。一些工作室更喜Ƣ在存储q程中进行大量编码,而另一些则喜欢在Java中实现其业务逻辑。每U方法各有优势和代h?br>  管如此Q还是有一些问题可促他们优先考虑其中的一U环境。在您的例子中,在数据库中执行大量计可能比数据加载到内存中更快,因此使用存储q程可能比较合理。另一斚wQ数据库承担q么多负载将对该应用E序的用户生负面媄响,因此最好付Z定代仯|络拉出q些数据Q以便将该数据库用作严格的存储系l,而不是计引擎。或者,如果应用E序的其余部分主要用JPAQ则适用的话Q可能希望用JPQL的大扚w更新功能来进行更新?br>  问题Q如果不先将数据加蝲到内存中Q是否可以执行大扚w更新?<br>  回答Q是的,可以通过JPQL执行大批量更新和大批量删除:<br>  UPDATE Employee e SET e.salary = e.salary * 1.1 WHERE e.salary < 100000 <br>  问题Q你们对Kodo JDO有什么规?JPA是否会通过实现JDO的所有功能而将其取?如果是的话,是否存在M旉?如果不是Q你们会不会l箋U极地开发JDO?<br>  回答QBEA仍然完全忠于JDO。从规范的观Ҏ看,我认一D|间之后,JPA包含当前的JDO规范中越来越多的功能。但是,我不了解Sun对JDO和JPA之间的融合工作有什么规划?br>问题Q什么是持久化单?<br>  回答Q持久化单元是类和配|设|的集合Q可以根据该集合创徏EntityManagerFactory。它?persistence.xml 文g中作Z个条目出现?br>  问题Q如何在WebLogic 9.2中测试JPA<br>  回答Q现在可以在WebLogic 9.2中用OpenJPA或Kodo。该服务器不执行会话bean持久化单元注入,但是?0.0服务器中可以q么作,q且?.2中,没有M Kodo控制台集成。但是除了引导注入问题之外,应该能够在WebLogic 9.2中成功地使用JPAQ包括参与托事务?br>  问题QJDBCq接对应于JPA中的什么概?<br>  回答QJPA EntityManager大致相当于JDBCq接Q而JPA EntityManagerFactory从概念上cM于JDBC数据源。JPA EntityTransaction(仅在JTA / appserver上下文以外可?相当于JDBCq接的事务控制API?br>  在OpenJPA中,EntityManager在其生命周期中可能用多个不同的JDBCq接。请参阅 openjpa.ConnectionRetainMode 属性的文档了解详细信息?br>  问题Q关于fetchcdQ如果默认是d(eager)加蝲Q则提供E序可能忽略惰?lazy)加蝲指o。因此,即ɞ字D设|ؓ惰性,也可能会加蝲不必要的数据。将来的规范会不会将其修改ؓ必须与fecthcd一?q会涉及C么问?<br>  回答Q通常QOpenJPA永远不会忽略用户配置的FetchMode。这是提C不是规则,因ؓ惰性加载实际上是调优过E中一关注事,永远都不应该对应用程序生行为性的影响*。JPA规范力图避免要求使用M明确的性能调优{略Q因Z同的|络拓扑l构、数据存储系l和应用E序行ؓ需要不同的调优x?br>  例如QOpenJPA允许在运行时 动态控?fetch配置。这意味着Q它可能静态地配置对象模型Q某些字段q行惰性加载,然后动态地其中一个字D|加到当前的fetch计划。这导致OpenJPAq反静态定义的惰性设|?br>  在当天结束时Q如果实现对数据加蝲执行错误的操作,您应能够非常L地评估其他实玎ͼ通过威胁转移到另一个实玎ͼ以至获得所需的功能。这是让大量供应商采用JPA规范的重大优势之一?br>  *当然Q如果您依靠惰性加载设|来防止加蝲某些数据Q以免后来传输到不同的层(也就是ؓ了数据安全?Q那么惰性加载存在重要的行ؓ性媄响。在OpenJPA中,可以使用 fetchl?控制通过늼发送数据图时确切地分离哪些数据?br>  问题Q在q行时更改fetch模式容不Ҏ?<br>  回答QJPA规范没有为此提供M工具。OpenJPA通过 fetch规划 接口提供了对fetch特征的详l控制。JPQL的“JOIN FETCH”结构也可以用于限制dfetch提示?br>  问题Q用乐观锁定时Q@Version注释仅支持int字段吗,它可以是datetime?<br>  回答Q根据JPA的要求,@Version可以对int、long、short、Integer、Short、Long和Timestampcd的字D用?JPA规范的第9.1.17节)?br>  问题Q在JPA可以调用存储q程?<br>  回答QJPA规范仅要求支持SELECT SQL语句(通过EntityManager.createNativeQuery()调用Q或@NamedNativeQuery注解或named- native-query XML元素)。但是,我认为大多数实现也多支持以相同方式调用存储q程?br>问题Q在EJB3中,更新实体bean的单个字D?列会D更新该DB行中的所有字D?列,q是仅更新该DB行中更改的列?<br>  回答Q该行ؓ取决于实现。OpenJPA只更新被修改字D对应的列。但是,我们可能在某些位|添加update-all-columns选项。请参阅 OPENJPA-38?br>  问题QEJB3.0如何替换EJB2.0中的ejbLoad()、ejbStore()之类的回调方?<br>  回答QJPA规范提供了一些可以随?单个)实现?回调Ҏ?br>  问题QEJB3.0如何替换EJB2.0 CMP和BMP?<br>  回答QEJB3 JPA规范对EJB2 CMP提供了功能完善的替换。JPA规范没有解决bean理的持久化Q如果您希望实现自己的持久化Q应该l用BMPQ或者最好用会话bean façadeq行自定义持久化?br>  问题Q命名查询可以位于JPA实体以外?像在会话bean或帮助类中那?<br>  回答QJPA实现仅扫描实体类(和映超cM及嵌入类)来查扑֑名查询。我希望来的JPA规范版本提供一U方式,用于命名查询限制到一个类对象中,到那个时候,可以认够在M位置定义命名查询?br>  可以在orm.xml文g中定义命名查询,然后使您的持久化单元指向该orm.xml文gQJPA规范允许Q意数目的orm.xml文g合ƈC赗?br>  问题QJPQL支持多数据库查询?<br>  回答QJPA规范q不要求实现必须只用单个数据库(甚至实现必须使用关系数据?。因此实现可以随意提供对多个数据库的讉K。但是,据我所知,当前的JPA实现都没有这么作Q除非是通过数据库方的工作来实现多数据库查询?br>  问题Q在JPQL中,SELECT子句可以从多个实体中拉出数据?<br>  回答Q是的。JPQL语言允许查询聚合和投影。因此以下语句是有效的JPQL语句Q?br>  select p.name, p.contactInfo.phoneNumber from Person p <br>  select p.address.state, avg(p.salary) from Person p group by p.address.state <br>  问题QJPA规范是否解决了缓存问?<br>  回答QJPA规范仅解决给定EntityManager相关对象的事务工作集的行为。它UC为“持久化上下文”。从某些斚w来讲Q这是一个缓存,但通常是ؓ了保持事务一致性,而不是ؓ了性能的原因?br>  JPA规范没有解决性能~存Q如OpenJPA?数据~存 ?查询~存。但是规范中的规则对q类性能~存暗示了某些行为约束?br>  总而言之,JPA规范主要x的仅是API的行为方面,而由各种实现完成大多数性能有关的调优。尽如此,所有可靠的实现都应该拥有某U数据缓存,以作为选择?br>  问题QWebLogic Server 9.0仍然仅支持EJB2.0Q是?<br>  回答Q正。WebLogic Server 10.0是完全支持EJB3规范的第一ƾBEA产品。在WebLogic Server 9中可以通过BEA Kodo产品来用JPA?br>问题Q关于JPA的推荐教E是什?<br>  回答QKodo文档 中提供了许多JPA教程?br>  问题Q是否存在Q何方式,用于跨所有实体表配置表前~?<br>  回答QJPA规范中没有提供这U方式,在OpenJPA中,可以通过创徏扩展?DBDictionary q写getValidTableName()Ҏ来实现该功能?br>  问题Q是否可能通过~程修改ORMl定(如重写orm.xml中指定的一些ORM配置)?<br>  回答Q不是通过JPA规范实现的。OpenJPA提供了一些方法,用于以编E的方式创徏映射信息Qƈ且该规范实提供了一U方法,用于在创建EntityManagerӞ特定于供应商的重写内容传递给persistence.xml中的数据?br>  问题Q我们正在构Z个大型应用程序,其中?50个对象坚持JPA规范。当我们使用Kodo 4.1持久化这些对象时Q它的SELECT查询最l将每个查询的大多数表连接v来,q得Kodo相当慢。TopLink Essentials实现仅连接少量的相关表。您对解册问题有什么徏?<br>  回答Q我认ؓq与“一对一”和“多对一”字D늱型的不同默认行ؓ有关。我猜想Q如果您明确地告知Kodo对“一对一”和“多对一”字D늱型执行惰性加载,׃很清楚。如果这不v作用Q或者如果您希望获得更多帮助来分析您的具体用例,请发送电子邮件到plinskey@bea.com?br>  问题Q开发h员可以用JPA来控制表的连接方式吗?<br>  回答Q不能直接控Ӟq且不是通过规范实现的。但是,大多数实现可能提供了一些方式来影响如何q接。有关OpenJPA的详l信息,请参阅关?dfetching 的文档?br>  问题Q在何处指定数据?<br>  回答Q数据源通常是在persistence.xml中指定的Q根据您的实现和应用服务器的默认行ؓQ可能需要ؓjta-data-source?或non-jta-data-source讄提供倹{?br>  问题QJPA规范是否计划在将来支持里E碑或双时态链(bi-temporal)?<br>  回答Q据我所知,JPA规范团队目前没有计划提供M时态功能?br>  问题Q如果抛Z观锁定异常,可以了解哪些列发生冲H吗<br>  回答Q不可以。您可以了解哪些实例p|Q但不是字段。给定失败的实例Q很Ҏ从数据库中加载新|q进行比较?img src ="http://www.tkk7.com/cherishchen/aggbug/131257.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/cherishchen/" target="_blank">凭栏观v</a> 2007-07-19 14:14 <a href="http://www.tkk7.com/cherishchen/archive/2007/07/19/131257.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ZJ2EE的Blogq_(?http://www.tkk7.com/cherishchen/archive/2007/07/16/130475.html凭栏观v凭栏观vMon, 16 Jul 2007 01:22:00 GMThttp://www.tkk7.com/cherishchen/archive/2007/07/16/130475.htmlhttp://www.tkk7.com/cherishchen/comments/130475.htmlhttp://www.tkk7.com/cherishchen/archive/2007/07/16/130475.html#Feedback0http://www.tkk7.com/cherishchen/comments/commentRss/130475.htmlhttp://www.tkk7.com/cherishchen/services/trackbacks/130475.htmlhttp://www.javaeedev.com/blog/article.jspx?articleId=ff80808112e766ee01130e1db01c0018

很清楚,q有源码下蝲



凭栏观v 2007-07-16 09:22 发表评论
]]>
Spring事务、异?/title><link>http://www.tkk7.com/cherishchen/archive/2007/07/04/128018.html</link><dc:creator>凭栏观v</dc:creator><author>凭栏观v</author><pubDate>Wed, 04 Jul 2007 01:30:00 GMT</pubDate><guid>http://www.tkk7.com/cherishchen/archive/2007/07/04/128018.html</guid><wfw:comment>http://www.tkk7.com/cherishchen/comments/128018.html</wfw:comment><comments>http://www.tkk7.com/cherishchen/archive/2007/07/04/128018.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/cherishchen/comments/commentRss/128018.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/cherishchen/services/trackbacks/128018.html</trackback:ping><description><![CDATA[<p class="MsoNormal" style="text-indent: 21pt;"><span lang="EN-US">Spring</span><span style="font-family: 宋体;">的事务实现采用基?/span><span lang="EN-US">AOP</span><span style="font-family: 宋体;">的拦截器来实玎ͼ如果没有在事务配|的时候注明回滚的</span><span lang="EN-US">checked exception</span><span style="font-family: 宋体;">Q那么只有在发生?/span><span lang="EN-US">unchecked exception</span><span style="font-family: 宋体;">的时候,才会q行事务回滚。因此在</span><span lang="EN-US">DAO</span><span style="font-family: 宋体;">层和</span><span lang="EN-US">service</span><span style="font-family: 宋体;">层,最好抛?/span><span lang="EN-US">unckecked exception</span><span style="font-family: 宋体;">Q毕竟对于数据库操作Q?/span><span lang="EN-US">unckecked exception</span><span style="font-family: 宋体;">更加合适,q个斚w的例?/span><span lang="EN-US">hibernate</span><span style="font-family: 宋体;">是一个,?/span><span lang="EN-US">hibernate2</span><span style="font-family: 宋体;">中,</span><span lang="EN-US">HibernateException</span><span style="font-family: 宋体;">q是</span><span lang="EN-US">checked exceptions</span><span style="font-family: 宋体;">Q但是到?/span><span lang="EN-US">hibernate3</span><span style="font-family: 宋体;">中就成了</span><span lang="EN-US">unchecked exceptions</span><span style="font-family: 宋体;">Q因为对于数据库操作来说Q一旦出现异常,是比较严重的错误,而且?/span><span lang="EN-US">client</span><span style="font-family: 宋体;">端基本上是无能ؓ力的Q所以?/span><span lang="EN-US">unchecked exceptions</span><span style="font-family: 宋体;">更加合适?/span></p> <p class="MsoNormal" style="text-indent: 21pt;"><span style="font-family: 宋体;">另外Q在</span><span lang="EN-US">DAO</span><span style="font-family: 宋体;">?/span><span lang="EN-US">service</span><span style="font-family: 宋体;">层的代码中,除非是ؓ了异常的转化、重新抛出,否则不要捕捉和处理异常,否则</span><span lang="EN-US">AOP</span><span style="font-family: 宋体;">在拦截的时候就不能捕捉到异常,也就不能正确执行回滚。这一炚w常很容易被忽视Q只有在明白?/span><span lang="EN-US">spring</span><span style="font-family: 宋体;">的事务处理机制后Q才能领会到?/span></p> <p class="MsoNormal" style="text-indent: 21pt;"><span style="font-family: 宋体;">对于</span><span lang="EN-US">hibernate</span><span style="font-family: 宋体;">的异常,</span><span lang="EN-US">spring</span><span style="font-family: 宋体;">会包?/span><span lang="EN-US">hibernate</span><span style="font-family: 宋体;">?/span><span lang="EN-US">upckecked hibernateException</span><span style="font-family: 宋体;">?/span><span lang="EN-US">DAOAccessException</span><span style="font-family: 宋体;">Qƈ且抛出,在事务管理层Q一旦接收到</span><span lang="EN-US">DAOAccessException</span><span style="font-family: 宋体;">׃触发事务的回滚,同时该异怼l箋向上层抛出,供上层进一步处理,比如?/span><span lang="EN-US">UI</span><span style="font-family: 宋体;">层向用户反馈错误信息{?/span></p> <span style="font-size: 10.5pt; font-family: 宋体;">下面的来?/span><span style="font-size: 10.5pt; font-family: "times new roman";" lang="EN-US">spring</span><span style="font-size: 10.5pt; font-family: 宋体;">参考手册的例子说明?/span><span style="font-size: 10.5pt; font-family: "times new roman";" lang="EN-US">spring</span><span style="font-size: 10.5pt; font-family: 宋体;">的事务和异常的关p,Z更好地说明问题,我修改了部分代码Q?br> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">package</span><span style="color: #000000;"> x.y.service;<br></span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.springframework.dao.DataAccessException;<br></span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.springframework.dao.PermissionDeniedDataAccessException;<br><br></span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> DefaultFooService </span><span style="color: #0000ff;">implements</span><span style="color: #000000;"> FooService {<br>    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Foo getFoo(String fooName) {<br>        </span><span style="color: #0000ff;">throw</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> UnsupportedOperationException();<br>    }<br><br>    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Foo getFoo(String fooName, String barName) {<br>        </span><span style="color: #0000ff;">throw</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> UnsupportedOperationException();<br>    }<br><br>    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> insertFoo(Foo foo)</span><span style="color: #0000ff;">throws</span><span style="color: #000000;"> DataAccessException {<br>        </span><span style="color: #0000ff;">throw</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> PermissionDeniedDataAccessException(</span><span style="color: #000000;">"</span><span style="color: #000000;">执行事务操作时发生异?/span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> UnsupportedOperationException());<br>    }<br><br>    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> updateFoo(Foo foo) {<br>        </span><span style="color: #0000ff;">throw</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> UnsupportedOperationException();<br>    }<br>}<br></span></div> <br> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">package</span><span style="color: #000000;"> x.y.service;<br></span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.springframework.context.ApplicationContext;<br></span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.springframework.context.support.ClassPathXmlApplicationContext;<br></span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.springframework.dao.DataAccessException;<br><br></span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">final</span><span style="color: #000000;"> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Boot {<br><br>    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> main(</span><span style="color: #0000ff;">final</span><span style="color: #000000;"> String[] args) </span><span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception {<br>        ApplicationContext ctx </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> ClassPathXmlApplicationContext(<br>                </span><span style="color: #000000;">"</span><span style="color: #000000;">applicationContext.xml</span><span style="color: #000000;">"</span><span style="color: #000000;">,Boot.</span><span style="color: #0000ff;">class</span><span style="color: #000000;">);<br>        FooService fooService </span><span style="color: #000000;">=</span><span style="color: #000000;"> (FooService) ctx.getBean(</span><span style="color: #000000;">"</span><span style="color: #000000;">fooService</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br>        </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {<br>            fooService.insertFoo(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Foo());<br>        } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (DataAccessException e) {<br>            System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">事务操作出现异常</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br>        }<br>        <br>        <br>    }<br>}<br></span></div> </span> <p class="MsoNormal"><span style="font-family: 宋体;">q里Q当</span><span lang="EN-US">Boot</span><span style="font-family: 宋体;">对象调用</span><span lang="EN-US">FooService</span><span style="font-family: 宋体;">来进行事务操作时Q由于在事务操作时抛Z</span><span lang="EN-US">unchecked exception</span><span style="font-family: 宋体;">Q被</span><span lang="EN-US">Spring</span><span style="font-family: 宋体;">?/span><span lang="EN-US">AOP</span><span style="font-family: 宋体;">事务处理模块拦截Q触发了事务的回滚,同时最l在控制C打出?#8220;事务操作出现异常”Q说?/span><span lang="EN-US">spring</span><span style="font-family: 宋体;">在触发了数据库回滚的同时又重新抛Z该异常?/span><span lang="EN-US"><o:p> <br></o:p></span></p> <span style="font-size: 10.5pt; font-family: 宋体;">Z更好地看?/span><span style="font-size: 10.5pt; font-family: "times new roman";" lang="EN-US">spring</span><span style="font-size: 10.5pt; font-family: 宋体;">事务拦截的过E,日志模式调?/span><span style="font-size: 10.5pt; font-family: "times new roman";" lang="EN-US">debug</span><span style="font-size: 10.5pt; font-family: 宋体;">模式<br> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">package</span><span style="color: #000000;"> x.y.service;<br><br></span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Foo {<br>}<br></span></div> <br> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">package</span><span style="color: #000000;"> x.y.service;<br><br></span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.springframework.dao.DataAccessException;<br><br></span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">interface</span><span style="color: #000000;"> FooService {<br>    <br>    Foo getFoo(String fooName);<br><br>    Foo getFoo(String fooName, String barName);<br><br>    </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> insertFoo(Foo foo)</span><span style="color: #0000ff;">throws</span><span style="color: #000000;"> DataAccessException;<br><br>    </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> updateFoo(Foo foo)</span><span style="color: #0000ff;">throws</span><span style="color: #000000;"> DataAccessException;<br>}<br></span></div> <br> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;"><?</span><span style="color: #ff00ff;">xml version="1.0" encoding="UTF-8"</span><span style="color: #0000ff;">?></span><span style="color: #000000;"><br></span><span style="color: #0000ff;"><</span><span style="color: #800000;">beans </span><span style="color: #ff0000;">xmlns</span><span style="color: #0000ff;">="http://www.springframework.org/schema/beans"</span><span style="color: #ff0000;"><br>    xmlns:xsi</span><span style="color: #0000ff;">="http://www.w3.org/2001/XMLSchema-instance"</span><span style="color: #ff0000;"><br>    xmlns:aop</span><span style="color: #0000ff;">="http://www.springframework.org/schema/aop"</span><span style="color: #ff0000;"><br>    xmlns:tx</span><span style="color: #0000ff;">="http://www.springframework.org/schema/tx"</span><span style="color: #ff0000;"><br>    xsi:schemaLocation</span><span style="color: #0000ff;">="<br>       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd<br>       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd<br>       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br><br><br><br>    </span><span style="color: #008000;"><!--</span><span style="color: #008000;"> this is the service object that we want to make transactional </span><span style="color: #008000;">--></span><span style="color: #000000;"><br>    </span><span style="color: #0000ff;"><</span><span style="color: #800000;">bean </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="fooService"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="x.y.service.DefaultFooService"</span><span style="color: #ff0000;"> </span><span style="color: #0000ff;">/></span><span style="color: #000000;"><br><br>    </span><span style="color: #008000;"><!--</span><span style="color: #008000;"> the transactional advice (i.e. what 'happens'; see the <aop:advisor/> bean below) </span><span style="color: #008000;">--></span><span style="color: #000000;"><br>    </span><span style="color: #0000ff;"><</span><span style="color: #800000;">tx:advice </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="txAdvice"</span><span style="color: #ff0000;"> transaction-manager</span><span style="color: #0000ff;">="txManager"</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br>        </span><span style="color: #008000;"><!--</span><span style="color: #008000;"> the transactional semantics<img src="http://www.tkk7.com/Images/dot.gif"> </span><span style="color: #008000;">--></span><span style="color: #000000;"><br>        </span><span style="color: #0000ff;"><</span><span style="color: #800000;">tx:attributes</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br>            </span><span style="color: #008000;"><!--</span><span style="color: #008000;"> all methods starting with 'get' are read-only </span><span style="color: #008000;">--></span><span style="color: #000000;"><br>            </span><span style="color: #0000ff;"><</span><span style="color: #800000;">tx:method </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="get*"</span><span style="color: #ff0000;"> read-only</span><span style="color: #0000ff;">="true"</span><span style="color: #ff0000;"> </span><span style="color: #0000ff;">/></span><span style="color: #000000;"><br><br>            </span><span style="color: #008000;"><!--</span><span style="color: #008000;"> other methods use the default transaction settings (see below) </span><span style="color: #008000;">--></span><span style="color: #000000;"><br>            </span><span style="color: #0000ff;"><</span><span style="color: #800000;">tx:method </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="*"</span><span style="color: #ff0000;"> </span><span style="color: #0000ff;">/></span><span style="color: #000000;"><br>        </span><span style="color: #0000ff;"></</span><span style="color: #800000;">tx:attributes</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br>    </span><span style="color: #0000ff;"></</span><span style="color: #800000;">tx:advice</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br><br>    </span><span style="color: #008000;"><!--</span><span style="color: #008000;"> ensure that the above transactional advice runs for any execution<br>        of an operation defined by the FooService interface </span><span style="color: #008000;">--></span><span style="color: #000000;"><br>    </span><span style="color: #0000ff;"><</span><span style="color: #800000;">aop:config</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br>        </span><span style="color: #0000ff;"><</span><span style="color: #800000;">aop:pointcut </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="fooServiceOperation"</span><span style="color: #ff0000;"><br>            expression</span><span style="color: #0000ff;">="execution(* x.y.service.FooService.*(..))"</span><span style="color: #ff0000;"> </span><span style="color: #0000ff;">/></span><span style="color: #000000;"><br>        </span><span style="color: #0000ff;"><</span><span style="color: #800000;">aop:advisor </span><span style="color: #ff0000;">advice-ref</span><span style="color: #0000ff;">="txAdvice"</span><span style="color: #ff0000;"><br>            pointcut-ref</span><span style="color: #0000ff;">="fooServiceOperation"</span><span style="color: #ff0000;"> </span><span style="color: #0000ff;">/></span><span style="color: #000000;"><br>    </span><span style="color: #0000ff;"></</span><span style="color: #800000;">aop:config</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br><br>    </span><span style="color: #008000;"><!--</span><span style="color: #008000;"> don't forget the DataSource </span><span style="color: #008000;">--></span><span style="color: #000000;"><br>    </span><span style="color: #0000ff;"><</span><span style="color: #800000;">bean </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="dataSource"</span><span style="color: #ff0000;"><br>        class</span><span style="color: #0000ff;">="org.apache.commons.dbcp.BasicDataSource"</span><span style="color: #ff0000;"><br>        destroy-method</span><span style="color: #0000ff;">="close"</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br>        </span><span style="color: #0000ff;"><</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="driverClassName"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="org.h2.Driver"</span><span style="color: #ff0000;"> </span><span style="color: #0000ff;">/></span><span style="color: #000000;"><br>        </span><span style="color: #0000ff;"><</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="url"</span><span style="color: #ff0000;"><br>            value</span><span style="color: #0000ff;">="jdbc:h2:tcp://localhost/D:/try/data/sample;IFEXISTS=TRUE"</span><span style="color: #ff0000;"> </span><span style="color: #0000ff;">/></span><span style="color: #000000;"><br>        </span><span style="color: #0000ff;"><</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="username"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="sa"</span><span style="color: #ff0000;"> </span><span style="color: #0000ff;">/></span><span style="color: #000000;"><br>        </span><span style="color: #0000ff;"><</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="password"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="123456"</span><span style="color: #ff0000;"> </span><span style="color: #0000ff;">/></span><span style="color: #000000;"><br>    </span><span style="color: #0000ff;"></</span><span style="color: #800000;">bean</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br><br>    </span><span style="color: #008000;"><!--</span><span style="color: #008000;"> similarly, don't forget the PlatformTransactionManager </span><span style="color: #008000;">--></span><span style="color: #000000;"><br>    </span><span style="color: #0000ff;"><</span><span style="color: #800000;">bean </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="txManager"</span><span style="color: #ff0000;"><br>        class</span><span style="color: #0000ff;">="org.springframework.jdbc.datasource.DataSourceTransactionManager"</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br>        </span><span style="color: #0000ff;"><</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="dataSource"</span><span style="color: #ff0000;"> ref</span><span style="color: #0000ff;">="dataSource"</span><span style="color: #ff0000;"> </span><span style="color: #0000ff;">/></span><span style="color: #000000;"><br>    </span><span style="color: #0000ff;"></</span><span style="color: #800000;">bean</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br>    </span><span style="color: #008000;"><!--</span><span style="color: #008000;"> other <bean/> definitions here </span><span style="color: #008000;">--></span><span style="color: #000000;"><br></span><span style="color: #0000ff;"></</span><span style="color: #800000;">beans</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br></span></div> <br></span> <h2><span style="font-size: 10.5pt; font-family: 宋体;">l论</span></h2> 在spring的事务管理环境下Q用unckecked exception可以极大地简化异常的处理Q只需要在事务层声明可能抛出的异常Q这里的异常可以是自定义的unckecked exception体系Q,在所有的中间层都只是需要简单throws卛_Q不需要捕捉和处理Q直接到最高层Q比如UI层再q行异常的捕捉和处理<br><br> <h2><span style="font-size: 10.5pt; font-family: 宋体;"></span></h2> <span style="font-size: 10.5pt; font-family: 宋体;"><br></span> <img src ="http://www.tkk7.com/cherishchen/aggbug/128018.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/cherishchen/" target="_blank">凭栏观v</a> 2007-07-04 09:30 <a href="http://www.tkk7.com/cherishchen/archive/2007/07/04/128018.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>spring学习日志-事务理http://www.tkk7.com/cherishchen/archive/2007/07/03/127860.html凭栏观v凭栏观vTue, 03 Jul 2007 07:26:00 GMThttp://www.tkk7.com/cherishchen/archive/2007/07/03/127860.htmlhttp://www.tkk7.com/cherishchen/comments/127860.htmlhttp://www.tkk7.com/cherishchen/archive/2007/07/03/127860.html#Feedback0http://www.tkk7.com/cherishchen/comments/commentRss/127860.htmlhttp://www.tkk7.com/cherishchen/services/trackbacks/127860.html在service层控制事?br>

Spring推荐事务的控制在service层实玎ͼ而不是像单独使用hibernate那样Q在dao层实玎ͼq样的好处在于更好地支持declear方式的事务(service层通常提供接口Q这样可以针Ҏ口来定义事务{略Q而不是针对具体的service实现Q因此可以很方便地替换service的实玎ͼ而不需要修改事务的配置Q,更加灉|。同Ӟ可以更加斚w自然地实现全局事务的控制?/span>

举例如下Q?/p> service接口

package x.y.service;
public interface FooService {
    
    Foo getFoo(String fooName);

    Foo getFoo(String fooName, String barName);

    
void insertFoo(Foo foo);

    
void updateFoo(Foo foo);
}

 


一个service实现

package x.y.service;
public class DefaultFooService implements FooService {
    
public Foo getFoo(String fooName) {
        
throw new UnsupportedOperationException();
    }

    
public Foo getFoo(String fooName, String barName) {
        
throw new UnsupportedOperationException();
    }

    
public void insertFoo(Foo foo) {
        
throw new UnsupportedOperationException();
    }

    
public void updateFoo(Foo foo) {
        
throw new UnsupportedOperationException();
    }
}

 

spring.xmlQ注意在声明事务的时候,是针对service接口的,而不是针对service实现?br>

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop
="http://www.springframework.org/schema/aop"
    xmlns:tx
="http://www.springframework.org/schema/tx"
    xsi:schemaLocation
="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
>



    
<!-- this is the service object that we want to make transactional -->
    
<bean id="fooService" class="x.y.service.DefaultFooService" />

    
<!-- the transactional advice (i.e. what 'happens'; see the <aop:advisor/> bean below) -->
    
<tx:advice id="txAdvice" transaction-manager="txManager">
        
<!-- the transactional semantics -->
        
<tx:attributes>
            
<!-- all methods starting with 'get' are read-only -->
            
<tx:method name="get*" read-only="true" />

            
<!-- other methods use the default transaction settings (see below) -->
            
<tx:method name="*" />
        
</tx:attributes>
    
</tx:advice>

    
<!-- ensure that the above transactional advice runs for any execution
        of an operation defined by the FooService interface 
-->
    
<aop:config>
        
<aop:pointcut id="fooServiceOperation"
            expression
="execution(* x.y.service.FooService.*(..))" />
        
<aop:advisor advice-ref="txAdvice"
            pointcut-ref
="fooServiceOperation" />
    
</aop:config>

    
<!-- don't forget the DataSource -->
    
<bean id="dataSource"
        class
="org.apache.commons.dbcp.BasicDataSource"
        destroy-method
="close">
        
<property name="driverClassName" value="org.h2.Driver" />
        
<property name="url"
            value
="jdbc:h2:tcp://localhost/D:/try/data/sample;IFEXISTS=TRUE" />
        
<property name="username" value="sa" />
        
<property name="password" value="123456" />
    
</bean>

    
<!-- similarly, don't forget the PlatformTransactionManager -->
    
<bean id="txManager"
        class
="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
<property name="dataSource" ref="dataSource" />
    
</bean>
    
<!-- other <bean/> definitions here -->
</beans>

 

使用service层方便了使用全局事务

对于横跨多个Hibernate SessionFacotry的分布式事务Q只需单地?/span> JtaTransactionManager 同多?/span> LocalSessionFactoryBean 的定义结合v来作Z务策略。你的每一?/span>DAO通过bean属性得到各自的 SessionFactory 引用。在需要进行跨多个数据库的事务操作Ӟ只需要定义一?/span>service层,使用 JtaTransactionManager 作ؓ事务{略Q横跨多?/span>DAO和多?/span>session factories来划分事务,而不需要特D的处理?/span>

CZ的配|文件如下:

<beans>
  
<bean id="mySessionFactory1" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    
<property name="dataSource" ref="myDataSource1"/>
   ..
  
</bean>
  
<bean id="mySessionFactory2" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    
<property name="dataSource" ref="myDataSource2"/>
 …
  
</bean>
  
<bean id="myTxManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
  
<bean id="myProductDao" class="product.ProductDaoImpl">
    
<property name="sessionFactory" ref="mySessionFactory1"/>
  
</bean>
  
<bean id="myInventoryDao" class="product.InventoryDaoImpl">
    
<property name="sessionFactory" ref="mySessionFactory2"/>
  
</bean>

<aop:config>
    
<aop:pointcut id="productServiceMethods" expression="execution(* product.ProductService.*(..))"/>
    
<aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods"/>
 
</aop:config>

  
<tx:advice id="txAdvice" transaction-manager="myTxManager">
    
<tx:attributes>
      
<tx:method name="increasePrice*" propagation="REQUIRED"/>
      
<tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW"/>
      
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
    
</tx:attributes>
  
</tx:advice>

    
<bean class="product.ProductServiceImpl">
       
<property name="productDao" ref="myProductDao"/>
       
<property name="inventoryDao" ref="myInventoryDao"/>
    
</bean>
    
</beans>






凭栏观v 2007-07-03 15:26 发表评论
]]>
javaeyeQ讨论:关于利用spring截获异常 然后throw自己的Exceptionshttp://www.tkk7.com/cherishchen/archive/2007/07/01/127369.html凭栏观v凭栏观vSun, 01 Jul 2007 09:00:00 GMThttp://www.tkk7.com/cherishchen/archive/2007/07/01/127369.htmlhttp://www.tkk7.com/cherishchen/comments/127369.htmlhttp://www.tkk7.com/cherishchen/archive/2007/07/01/127369.html#Feedback0http://www.tkk7.com/cherishchen/comments/commentRss/127369.htmlhttp://www.tkk7.com/cherishchen/services/trackbacks/127369.html
http://www.javaeye.com/topic/18152?page=1

http://www.javaeye.com/topic/41820




凭栏观v 2007-07-01 17:00 发表评论
]]>
y好用的H2数据?/title><link>http://www.tkk7.com/cherishchen/archive/2007/06/27/126524.html</link><dc:creator>凭栏观v</dc:creator><author>凭栏观v</author><pubDate>Wed, 27 Jun 2007 03:20:00 GMT</pubDate><guid>http://www.tkk7.com/cherishchen/archive/2007/06/27/126524.html</guid><wfw:comment>http://www.tkk7.com/cherishchen/comments/126524.html</wfw:comment><comments>http://www.tkk7.com/cherishchen/archive/2007/06/27/126524.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/cherishchen/comments/commentRss/126524.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/cherishchen/services/trackbacks/126524.html</trackback:ping><description><![CDATA[<p><a title="http://www.h2database.com/html/frame.html" >http://www.h2database.com/html/frame.html</a></p> <p> </p> <p>最q一个桌面的E序需要用嵌入式数据库,选择了很多不同的嵌入式数?最l选择了H2Q性能是一个很重要的原因,下图是h2提供的Performance的比较图</p> <p><a title="http://www.h2database.com/html/images/performance.png" >http://www.h2database.com/html/images/performance.png</a></p> <p> </p> <p>h2是Thomas Mueller提供的一个开源的、纯java实现的关pL据库Q在google上面有讨论组<a href="mailto:h2-database@googlegroups.com">h2-database@googlegroups.com</a>QThomas Mueller也非常热情,回答问题非常及时?/p> <p> </p> <p>下面说下我最l选择h2的原因:</p> <p>Q?Q性能、小?/p> <p>Q?Q同时支持网l版和嵌入式版本Q另外还提供了内存版</p> <p>Q?Q有比较好的兼容性,支持相当标准的sql标准Q实际上也不存在一个数据库能够100%W合标准Q?/p> <p>Q?Q提供了非常友好的基于web的数据库理界面</p> <p>Q?Q支持hibernate</p> <br> <p><br></p> 补充Q?br>支持的事务隔ȝ别:<br><br> <h2>Transaction <span id="firstFound" style="color: #000000; background-color: #ffff00;">Isolation</span></h2> <p> This database supports the following transaction <span id="firstFound" style="color: #000000; background-color: #ffff00;">isolation</span> <span style="color: #000000; background-color: #00ffff;">level</span>s: </p> <ul> <li><strong>Serializable</strong><br> This is the default <span style="color: #000000; background-color: #00ffff;">level</span>.<br> To enable, execute the SQL statement 'SET LOCK_MODE 0'<br> or append ;LOCK_MODE=1 to the database URL: jdbc:h2:~/test;LOCK_MODE=1 </li> <li><strong>Read Committed</strong><br> Read locks are released immediately. Higher concurrency is possible when using this <span style="color: #000000; background-color: #00ffff;">level</span>.<br> This is the <span id="firstFound" style="color: #000000; background-color: #ffff00;">isolation</span> <span style="color: #000000; background-color: #00ffff;">level</span> used for many database systems.<br> To enable, execute the SQL statement 'SET LOCK_MODE 0'<br> or append ;LOCK_MODE=3 to the database URL: jdbc:h2:~/test;LOCK_MODE=3 </li> <li><strong>Read Uncommitted</strong><br> This <span style="color: #000000; background-color: #00ffff;">level</span> means that transaction <span id="firstFound" style="color: #000000; background-color: #ffff00;">isolation</span> is disabled.<br> To enable, execute the SQL statement 'SET LOCK_MODE 0'<br> or append ;LOCK_MODE=0 to the database URL: jdbc:h2:~/test;LOCK_MODE=0 </li> </ul> <p> When using the <span id="firstFound" style="color: #000000; background-color: #ffff00;">isolation</span> <span style="color: #000000; background-color: #00ffff;">level</span> 'serializable', dirty reads, non-repeatable reads, and phantom reads are prohibited. </p> <ul> <li><strong>Dirty Reads</strong><br> Means a connection can read uncommitted changes made by another connection.<br> Possible with: read uncommitted </li> <li><strong>Non-Repeatable Reads</strong><br> A connection reads a row, another connection changes a row and commits, and the first connection re-reads the same row and gets the new result.<br> Possible with: read uncommitted, read committed </li> <li><strong>Phantom Reads</strong><br> A connection reads a set of rows using a condition, another connection inserts a row that falls in this condition and commits, then the first connection re-reads using the same condition and gets the new row.<br> Possible with: read uncommitted, read committed </li> </ul> <h3>Table <span style="color: #000000; background-color: #00ffff;">Level</span> Locking</h3> The database allows multiple concurrent connections to the same database. To make sure all connections only see consistent data, table <span style="color: #000000; background-color: #00ffff;">level</span> locking is used. This mechanism does not allow high concurrency, but is very fast. Shared locks and exclusive locks are supported. Before reading from a table, the database tries to add a shared lock to the table (this is only possible if there is no exclusive lock on the object by another connection). If the shared lock is added successfully, the table can be read. It is allowed that other connections also have a shared lock on the same object. If a connection wants to write to a table (update or delete a row), an exclusive lock is required. To get the exclusive lock, other connection must not have any locks on the object. After the connection commits, all locks are released. This database keeps all locks in memory.<br> <img src ="http://www.tkk7.com/cherishchen/aggbug/126524.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/cherishchen/" target="_blank">凭栏观v</a> 2007-06-27 11:20 <a href="http://www.tkk7.com/cherishchen/archive/2007/06/27/126524.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>javascript tipshttp://www.tkk7.com/cherishchen/archive/2007/06/26/126352.html凭栏观v凭栏观vTue, 26 Jun 2007 07:58:00 GMThttp://www.tkk7.com/cherishchen/archive/2007/06/26/126352.htmlhttp://www.tkk7.com/cherishchen/comments/126352.htmlhttp://www.tkk7.com/cherishchen/archive/2007/06/26/126352.html#Feedback0http://www.tkk7.com/cherishchen/comments/commentRss/126352.htmlhttp://www.tkk7.com/cherishchen/services/trackbacks/126352.htmlThe Browser Object Model




The window object represents an entire browser window



Document Object Model (DOM)


The Document Object Model (DOM) is perhaps the single greatest innovation on the Web since
HTML was first used to connect related documents together over the Internet. The DOM gives
developers unprecedented access to HTML, enabling them to manipulate and view HTML as an
XML document. The DOM represents the evolution of Dynamic HTML, pioneered by Microsoft
and Netscape, into a true cross-platform, language-independent solution.

Note that the DOM is a language-independent API, meaning that it is not tied to
Java, JavaScript, or any other language for implementation. For the purposes of this
book, however, I place most focus on the JavaScript implementation.




凭栏观v 2007-06-26 15:58 发表评论
]]>
一个模拟webwork的拦截器工作原理的例?(yuanchuang)http://www.tkk7.com/cherishchen/archive/2007/06/25/126150.html凭栏观v凭栏观vMon, 25 Jun 2007 08:45:00 GMThttp://www.tkk7.com/cherishchen/archive/2007/06/25/126150.htmlhttp://www.tkk7.com/cherishchen/comments/126150.htmlhttp://www.tkk7.com/cherishchen/archive/2007/06/25/126150.html#Feedback0http://www.tkk7.com/cherishchen/comments/commentRss/126150.htmlhttp://www.tkk7.com/cherishchen/services/trackbacks/126150.html /** 
  *  模拟webwork的拦截器
  
*/  
 
import java.util.ArrayList;  
 
import java.util.Iterator;  
   
 
public class ActionInvocation {  
  
public boolean executed = false;  
   
//q个intercepters必须是成员变?/span>
  protected Iterator intercepters;  
   
  
public String resultCode;  
   
  
public String invoke() throws Exception {  
   
if (executed) {  
    
throw new IllegalStateException("Action has already executed");  
   }  
   
   
if (intercepters.hasNext()) { 
    
// 因ؓintercepters是类成员变量Q所以递归执行到这里的时候,会执行一个Intercepter的interceptҎQ知道都q代完毕
    Intercepter intercepter = (Intercepter) intercepters.next(); 


    
//递归  
    
// here is recure point ,programm run to here  
    
// then save the state into the stack ,and jump to the top of the method run again  
    
// and so and so util the condition to else branch  
    resultCode = intercepter.intercept(this);  
      
   } 
else {  
    resultCode 
= invokeActionOnly();  
   }  
   
   
if (!executed) {  
      
    System.out.println(
"now it is time to run the action, and this method should not run again!!");  
    executed 
= true;  
   }  
   
return resultCode;  
  }  
   
  
private String invokeActionOnly() {  
   System.out.println(
"run invokeActionOnly() ");  
   
// invoke和intercept的递归的时候返回都是q个|所以这个返回D够保存到最后,  
   
// 只是在两个方法之间被多次C?nbsp; 
   return "here is action return value,it's the result;";  
  }  
   
  
public ActionInvocation(){  
   ArrayList ay
=new ArrayList();  
   ay.add(
new Intercepter(1));  
   ay.add(
new Intercepter(2));  
   ay.add(
new Intercepter(3));  
   intercepters
=ay.iterator();  
     
  }  
  
public static void main(String[] args) {  
   ActionInvocation actionInvocation 
=new ActionInvocation();  
   
try {  
    System.out.println(actionInvocation.invoke());  
   } 
catch (Exception e) {    
    e.printStackTrace();  
   }   
  }  
 } 



 public class Intercepter {  
  
private int id;  
  
public Intercepter(int id){  
   
this.id=id;  
  }  
    
  
public String intercept(ActionInvocation actionInvocation) throws Exception {  
   String result 
= null;  
   System.out.println(
"run the intercept()"+this.id);  
   String resultCode
=actionInvocation.invoke();  
    System.out.println(
"after the action run,run the intercept continue  "+this.id);  
   
return resultCode;  
  }  
    
 }



凭栏观v 2007-06-25 16:45 发表评论
]]>
使用h2的case函数实现交叉?/title><link>http://www.tkk7.com/cherishchen/archive/2007/06/25/126126.html</link><dc:creator>凭栏观v</dc:creator><author>凭栏观v</author><pubDate>Mon, 25 Jun 2007 07:52:00 GMT</pubDate><guid>http://www.tkk7.com/cherishchen/archive/2007/06/25/126126.html</guid><wfw:comment>http://www.tkk7.com/cherishchen/comments/126126.html</wfw:comment><comments>http://www.tkk7.com/cherishchen/archive/2007/06/25/126126.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/cherishchen/comments/commentRss/126126.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/cherishchen/services/trackbacks/126126.html</trackback:ping><description><![CDATA[数据?span style="font-size: 10pt; font-family: 宋体;" lang="EN-US">t的ddlQ?br>create table t(class char(1),callcount int,calldate date)<br></span><img src="http://www.tkk7.com/images/blogjava_net/cherishchen/a1.jpg" border="0"><br><span style="font-size: 10pt; font-family: 宋体;" lang="EN-US"><br>sql语句:</span> <p class="MsoNormal" style="margin: 6pt 0cm;"><span style="font-size: 10pt; font-family: 宋体;" lang="EN-US">select calldate,<o:p></o:p></span></p> <p class="MsoNormal" style="margin: 6pt 0cm;"><span style="font-size: 10pt; font-family: 宋体;" lang="EN-US">sum(case class when '1' then callcount else 0 end) as A</span><span style="font-size: 10pt; font-family: 宋体;">c?span lang="EN-US">,<o:p></o:p></span></span></p> <p class="MsoNormal" style="margin: 6pt 0cm;"><span style="font-size: 10pt; font-family: 宋体;" lang="EN-US">sum(case class when '2' then callcount else 0 end) as B</span><span style="font-size: 10pt; font-family: 宋体;">c?span lang="EN-US">,<o:p></o:p></span></span></p> <p class="MsoNormal" style="margin: 6pt 0cm;"><span style="font-size: 10pt; font-family: 宋体;" lang="EN-US">sum(case class when '3' then callcount else 0 end) as C</span><span style="font-size: 10pt; font-family: 宋体;">c?<span lang="EN-US"><o:p></o:p></span></span></p> <span style="font-size: 10pt; font-family: 宋体;" lang="EN-US">from t group by calldate<br><br>最后结果:<br><img src="http://www.tkk7.com/images/blogjava_net/cherishchen/a2.jpg" border="0"><br><br></span><span style="font-size: 10pt; font-family: 宋体;" lang="EN-US"><!--[if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter"/> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0"/> <v:f eqn="sum @0 1 0"/> <v:f eqn="sum 0 0 @1"/> <v:f eqn="prod @2 1 2"/> <v:f eqn="prod @3 21600 pixelWidth"/> <v:f eqn="prod @3 21600 pixelHeight"/> <v:f eqn="sum @0 0 1"/> <v:f eqn="prod @6 1 2"/> <v:f eqn="prod @7 21600 pixelWidth"/> <v:f eqn="sum @8 21600 0"/> <v:f eqn="prod @7 21600 pixelHeight"/> <v:f eqn="sum @10 21600 0"/> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/> <o:lock v:ext="edit" aspectratio="t"/> </v:shapetype><v:shape id="_x0000_i1025" type="#_x0000_t75" style="'width:120.75pt; height:96pt'"> <v:imagedata src="file:///C:\DOCUME~1\chen\LOCALS~1\Temp\msohtml1\01\clip_image001.png" o:title=""/> </v:shape><![endif]--><!--[if !vml]--><!--[endif]--></span><br> <img src ="http://www.tkk7.com/cherishchen/aggbug/126126.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/cherishchen/" target="_blank">凭栏观v</a> 2007-06-25 15:52 <a href="http://www.tkk7.com/cherishchen/archive/2007/06/25/126126.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <p>лǵվܻԴȤ</p> <a href="http://www.tkk7.com/" title="亚洲av成人片在线观看">亚洲av成人片在线观看</a> <div class="friend-links"> </div> </div> </footer> վ֩ģ壺 <a href="http://yu388.com" target="_blank">Ʒþþ</a>| <a href="http://jinluffcl.com" target="_blank">޶ۺϾþ</a>| <a href="http://tiantangapp.com" target="_blank">Ůˬ̼ƵѲ</a>| <a href="http://zjlxblog.com" target="_blank">Ļav</a>| <a href="http://dstbxg.com" target="_blank">ձ߹ۿƵ</a>| <a href="http://www-006688.com" target="_blank">2019Ļ6</a>| <a href="http://513net.com" target="_blank">Ʒһ߹ۿ</a>| <a href="http://avdaka.com" target="_blank">޾ƷƷþ99һ</a>| <a href="http://littlevv.com" target="_blank">þwww˳_Ƭ</a>| <a href="http://cztshw.com" target="_blank">ɫػaëƬѹۿ</a>| <a href="http://takiku.com" target="_blank">޹þþۺվ</a>| <a href="http://888-28.com" target="_blank">ִֻˬƵ</a>| <a href="http://bbby6.com" target="_blank">һѲ</a>| <a href="http://fholaer.com" target="_blank">޾ƷþþþAƬԾ</a>| <a href="http://hljjlhl.com" target="_blank">˳77777ɫ߲</a>| <a href="http://bentuxinli.com" target="_blank">aëƬȫƵ</a>| <a href="http://181xx.com" target="_blank">һػ¼Ѳŷ</a>| <a href="http://yy6653.com" target="_blank">H߲߹ۿH</a>| <a href="http://aplus178.com" target="_blank">ۺAVһ</a>| <a href="http://zanyoo.com" target="_blank">ëƬƵ</a>| <a href="http://xjdz8.com" target="_blank">AVһ</a>| <a href="http://caicpa.com" target="_blank">Ƶ߹ۿѲӰԺ</a>| <a href="http://jkc178.com" target="_blank">޳AVƬþ</a>| <a href="http://528676.com" target="_blank">޹þþþƷ</a>| <a href="http://xsdin.com" target="_blank">ѸƵվ</a>| <a href="http://pumanpig.com" target="_blank">91ƷƵѹۿ</a>| <a href="http://zzanfang.com" target="_blank">ѿƬ</a>| <a href="http://hairdehf.com" target="_blank">ŷһ</a>| <a href="http://watchmini.com" target="_blank">ް鵺̳</a>| <a href="http://fennenll.com" target="_blank">ձ߹ۿƵ</a>| <a href="http://8833655.com" target="_blank">ձһۺϾþa</a>| <a href="http://bd0574.com" target="_blank">Ʒþþþþþþ </a>| <a href="http://www003924.com" target="_blank">aëƬѲȫ</a>| <a href="http://miya863.com" target="_blank">޾avĻ</a>| <a href="http://805ios.com" target="_blank">һѹۿ</a>| <a href="http://53ggk.com" target="_blank">޾Ʒa vС˵</a>| <a href="http://www19977.com" target="_blank">ҹҹƷƵ</a>| <a href="http://vc77777.com" target="_blank">ѿƵվ</a>| <a href="http://9196528.com" target="_blank">41515hh</a>| <a href="http://33dh2.com" target="_blank">˳ɻҳ߹ۿձ</a>| <a href="http://hairdehf.com" target="_blank">ðѾƷƵ</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>