首页 | 源码下载 | 网站模板 | 网页特效 | 广告代码 | 网页素材 | 字体下载 | 书库 | 站长工具
会员投稿 投稿指南 RSS订阅
当前位置:主页>网络编程>java教程>资讯:Spring中的TopLink ServerSession

Spring中的TopLink ServerSession

www.jz123.cn  2009-08-28   来源:   IT专家网    责任编辑(袁袁)    我要投递新闻

  SessionFactory 抽象层

  TopLink本身并没有提供SessionFactory抽象层逻辑,多线程的数据访问是建立在中央 ServerSession 上的。对于单线程访问, 这个中央 ServerSession 会为它一个 ClientSession 的实例供其使用。为了提供灵活便捷的创建选项, Spring为TopLink定义了一个 SessionFactory 接口,从而使你可以任意地在不同的 Session 创建策略之间进行切换。

  作为一个一站式的商店,Spring提供了一个 LocalSessionFactoryBean 类,允许你以bean风格的配置方式来定义一个TopLink SessionFactory。 需要进行配置的地方主要是TopLink session配置文件,通常来说还需配置一个受到Spring管理的JDBC DataSource。

  


  • <beans>  

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource                                                            " destroy-method="close">  
        <property name="driverClassName" value="${jdbc.driverClassName}"/>  
        <property name="url" value="${jdbc.url}"/>  
        <property name="username" value="${jdbc.username}"/>  
        <property name="password" value="${jdbc.password}"/>  
    bean>  

    <bean id="mySessionFactory" class="org.springframework.orm.toplink.                                            LocalSessionFactoryBean">  
        <property name="configLocation" value="toplink-sessions.xml"/>  
        <property name="dataSource" ref="dataSource"/>  
    bean>  
          
    beans>  
    <toplink-configuration>  

    <session>  
        <name>Sessionname>  
        <project-xml>toplink-mappings.xmlproject-xml>  
        <session-type>  
          <server-session/>  
        session-type>  
        <enable-logging>trueenable-logging>  
        <logging-options/>  
    session>  

    toplink-configuration> 

        通常情况下,LocalSessionFactoryBean 在底层将持有一个多线程的TopLink ServerSession 并创建合适的客户端 Session: 它或者是一个普通的 Session(典型情况) —— 一个受管理的 ClientSession;或者是一个具备事务功能的 Session (后者主要在Spring内部对TopLink的支持中被使用)。还有一种情况,LocalSessionFactoryBean 可能会持有一个单线程的TopLink的 DatabaseSession,这是非常特殊的情况了。

      TopLinkTemplate & TopLinkDaoSupport

      每个基于TopLink的DAO将通过IoC被注入一个 SessionFactory,你可以通过Setter方式注入,也可以用构造函数方式注入。这样的DAO可以直接操作原生的TopLink API,通过 SessionFactory 来获取一个 Session, 但是通常情况下,你更愿意使用Spring的 TopLinkTemplate:


    <beans>  

    <bean id="myProductDao" class="product.ProductDaoImpl">  
        <property name="sessionFactory" ref="mySessionFactory"/>  
    bean>  

    beans>  
    public class TopLinkProductDao implements ProductDao {   

        private TopLinkTemplate tlTemplate;   

        public void setSessionFactory(SessionFactory sessionFactory) {   
            this.tlTemplate = new TopLinkTemplate(sessionFactory);   
        }   

        public Collection loadProductsByCategory(final String category)                                            throws DataAccessException {   
          return (Collection) this.tlTemplate.execute(new TopLinkCallback() {   
           public Object doInTopLink(Session session) throws TopLinkException {   
            ReadAllQuery findOwnersQuery = new ReadAllQuery(Product.class);   
            findOwnersQuery.addArgument("Category");   
            ExpressionBuilder builder = this.findOwnersQuery.getExpressionBuilder();   
             findOwnersQuery.setSelectionCriteria(   
                  builder.get("category").like(builder.getParameter("Category")));   

                    Vector args = new Vector();   
                    args.add(category);   
                    List result = session.executeQuery(findOwnersQuery, args);   
                    // do some further stuff with the result list   
                    return result;   
                }   
            } 
        }   

     


         public class ProductDaoImpl extends TopLinkDaoSupport implements ProductDao {
      public Collection loadProductsByCategory(String category) throws DataAccessException {
      ReadAllQuery findOwnersQuery = new ReadAllQuery(Product.class);
      findOwnersQuery.addArgument("Category");
      ExpressionBuilder builder = this.findOwnersQuery.getExpressionBuilder();
      findOwnersQuery.setSelectionCriteria(
      builder.get("category").like(builder.getParameter("Category")));
      return getTopLinkTemplate().executeQuery(findOwnersQuery, new Object[] {category});
      }
      }

      边注:TopLink查询对象是线程安全的,并且能够在DAO层被缓存。在一开始被创建时以实例变量的方式被保持。

      作为不使用Spring的 TopLinkTemplate 来实现DAO的替代解决方案, 你依然可以通过原生TopLink API对那些基于Spring的DAO进行编程,此时你必须明确地打开和关 闭一个 Session。正如在相应的Hibernate章节描述的一样,这种做法的主要优点在于你的数据访问代码可以在整个过程中抛出checked exceptions。 TopLinkDaoSupport 为这种情况提供了多种函数支持,包括获取和释放 一个具备事务的 Session 并做相关的异常转化。

  • 上一篇:Spring中的四种声明式事务的配置 下一篇:Eclipse RCP编辑器关闭按钮的屏蔽方法

    评论总数:0 [ 查看全部 ] 网友评论


    关于我们隐私版权广告服务友情链接联系我们网站地图