`
oak2008
  • 浏览: 77054 次
  • 性别: Icon_minigender_1
  • 来自: China
社区版块
存档分类
最新评论

用EJB3.0,不用再Spring+Hibernate

阅读更多
前天跟你说的那个JSF表现层遇到的问题我已经解决了!

我这个项目,业务层,我打算用EJB3.0,不用再Spring+Hibernate
原因如下
Spring的业务层的缓冲类要自己设计,设计是有些复杂的,而且设计也存在一个问题,照Put JSF to work那个例子那种做法,一次性都把数据表
里的数据都装进内存里,这样做有一个问题,试想,如果数据据库的每条记录的大小达1M(可以达到的,
比如有一个字段的是文本的,文字很多,像新闻发布系统),那么如果有1024 条记录,那把这个表的数据都装进内存,那么内存就要1G
,如果一个应用很多表呢!内存要多少,不可想象!

当然,如果用Spring+hibernate,理论上说,可以满足这个项目,

不过我们这次课程设计,不只是满足这个项目的需要就可以了,要重要的是要做到规范化的设计!因为这
次设计,将会是将来的设计的指导

我选择EJB3.0有几个原因:
1.EJB3.0的事务也是交给容器管理,自动rollback.
2.EJB3.0在netbeans 上开发非常容易!
3.EJB的组件有一个优点,它不是一次性把数据表的记录以EJB对象的形式都装进缓存,而是有需要的时候,才装进缓存,然后在缓存中,如果那个对象长时间不被用到,它就会自动从缓存中消失!这是我现在的大概理解!更详细的内容,看有关EJB生命周期的介绍!



分享到:
评论
14 楼 z_jordon 2007-09-13  
用ejb3.0即使web层和service层在同一机器,调用的时候也是要通过socket通信的,并发量大的时候,效率非常低,不知道这个问题你考虑过没有?
13 楼 oak2008 2007-09-12  
I agree with what you said!it seems i used to have a wrong thinking of ejb3.0's advantages.Because I leant about the lifecycle of ejb2.0,in my memory,ejb2.0's cached is managed by the ejb container,so it is in
the ejb3.0!now I should correct my understanding about ejb3.0...thank you for remind me of my wrong thinking and giving me so
valuable advice! by the way,could i make a friends with you? add my qq:441802232,my email:
oak_beijing2008@126.com
12 楼 fxy1949 2007-09-12  
I am a big fan of EJB3. But the advantages of EJB3 is not like you said.

Data cache is not supported by EJB3 container,but by its Entity Manager implementation,such as topLink or Hibernate,etc. It has nothing to do with EJB3 container. And for handling big data, your application should adopt some strategy for this issue, not container solve this problem for you. Moreover,transaction management is also supported by Spring+hibernate.

After one year practice,the main advantages of EJB3 I think are as following:

1) The business logic is totally seperated from presentation tier, and can be shared remotely by other sub-system or be wrappered as Web service for outside invocation in future SOA approach.

This is vital for complex enterprise applications.But for small,isolated,dedicated system,it's not a big deal.

2) EJB3 persistence(Entity bean part) is exactly the same with Hibernate3. All the knowledge and experience you got from Hibernate,such as HQL,entity manipulation, will still useful and valuable.


3) The ejb3 entity bean is pojos,which you can use them to communicate with presentaion layer, and DTO(Data Transfer Object) is no longer needed. The Struts or JSF cocomponent can directly refer to this entity bean(anyway,they are detached). After submitting,these detached entity beans can be sent to bussiness logic layer(session bean),without any conversion using DTO or whatever.

    <div class="dark col output-field">
         <h:outputText value="#{productBean.event.product.billingDescription}"/>
    </div>               
    <div class="dark col input-field">
         <h:selectOneMenu id="service" value="#{productBean.event.product.service.id}" validator="#{productBean.validateSelection}" required="true">
             <f:selectItems value="#{productBean.serviceList}"/>
          </h:selectOneMenu>
    </div>

4) In comparison with EJB2,EJB3 is really easy to write,POJOs+annotation.

@Entity
@Table(name = "CMM_PRODUCT_PRO")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "BUNDLE", discriminatorType = DiscriminatorType.STRING)

public class Product extends BasicBaseBean
{  
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ")
    @SequenceGenerator(name="SEQ", sequenceName="product_id", allocationSize=1)
    @Column(name = "ID")
    protected long id;
   
    @Column(name = "SORT_TITLE", nullable = false)
    private String sortTitle;
   
    @Column(name = "BILLING_DESCRIPTION", nullable = false)
    private String billingDescription;

   
    @JoinColumn(name = "SERVICE", referencedColumnName = "ID")
    @ManyToOne
    private Service service=new Service();

      
    @JoinTable(name = "CMM_PRODUCT_GENRE_PRG", joinColumns = {@JoinColumn(name = "PRO_ID", referencedColumnName = "ID")}, inverseJoinColumns =  {@JoinColumn(name = "GENRE", referencedColumnName = "ID")})
    @ManyToMany(fetch=FetchType.LAZY)
    private Set<Genre> genres;
   
    @OneToMany(mappedBy = "product", fetch = FetchType.LAZY)
    private Collection<ImageAsset> imageAssets;

    ...
    public Product() {
    }

    public long getId() {
        return this.id;
    }
   
    public void setId(long id)
    {
        this.id=id;
    }
    ...
}

...

@Stateless
@TransactionManagement(value = TransactionManagementType.CONTAINER)
public class ProductSession implements ProductSessionLocal,ProductSessionRemote

{
    static final Logger log = Logger.getLogger("ProductSession");

    @PersistenceContext(unitName = "oracle")
    private EntityManager em;


    @EJB
    private AdminSessionLocal adminSession;


    /**
     * Find a product entity by its ID
     */
    public Product findProductById(long id) throws AppException, Exception
    {
Product product=null;

try {
    product=(Product)em.find(Product.class,id);
    initProduct(product);
}
         catch(Exception e) {
    throw e;
}

return product;
    }


    /**
     * Create a product
     */
    public ProductResponse createProduct(ProductEvent pe) throws AppException, Exception
    {
Product product=pe.getProduct();

try
        {
    // set product data
    product.setType(CmmejbKeys.PRODUCT);
    product.setPriority((long) 0);
    product.setAdultContent(pe.getIsAdultContent());

    if (pe.product.getChannelBrand().getId()==0)
product.setChannelBrand(null);

    // save genres and moods into ProductGenre
    saveGenreAndMood(product, pe.getGenres(), pe.getMoods());

    em.persist(product);
    em.flush(); // save into database

    em.refresh(product); // refresh from database

    initProduct(product); // initialize product
}
         catch (Exception e) {
    throw e;
}

return new ProductResponse(product);
    }
...
}


5) Thre are more ...

11 楼 江南白衣 2007-09-12  
如果EJB3能成大气,能发展足够的用户量,Glassfish+Netbeans+EJB3这样的傻瓜套餐的开发速度的确比Spring+Hibernate快一些,说起来也规范些。就怕它跑到半路跪倒了,还有其他一些唯Spring是举的OSS对EJB3的支持怎样。
10 楼 ray_linn 2007-09-12  
新闻系统?用啥ejb和hibernate,既然文字都有一M,还不如就用FreeMarker生成静态文本呢。

新闻系统又不是啥事务敏感系统,用得着EJB或者Spring AOP么?


真是瞎胡闹。
9 楼 oak2008 2007-09-12  
EJB3.0我并没有选择Jboss的那个hibernate实现版,我选择的是sun公司提倡的TopLink的标准版,服务器我也不用jboss,它已经收费了,我选择的是glassfish!
8 楼 williamy 2007-09-12  
EJB3,关注了很久,但偶只会用而已,为什么,因为Spring的代码很明确在Spring提供的包里,而EJB的呢?JBOSS里找,JBoss的代码很乱乱,我不喜欢,至于EJB3还是Spring的功能,呵呵,毕竟都是框架而已,功能有没有是另外一回事,偶觉得框架在开发过程只是决定了你的开发方式,至于功能是你自己的事情
7 楼 oak2008 2007-09-12  
用EJB3.0访问很多记录,我的测试里是3000记录,我昨晚已经做过测试,性能还是很好,500ms左右.用以前设计的Cached(Spring),用时间大概是70ms!这个差距不到一秒,考虑到上的原因,用EJB3.0还是合算的!
6 楼 差沙 2007-09-12  
这个是存在map里面,又能说明什么呢?这就能说明:
“Spring的业务层的缓冲类要自己设计,设计是有些复杂的”了?
5 楼 nihongye 2007-09-12  
ejb3压根没提二级缓存,一级缓存与hibernate相同。
ejb3持久那块不是不好,只是没有hibernate包含的多
4 楼 惊鸿逝水 2007-09-12  
都说了是Demo了,自己写个基于LRU Cache也很简单,再说spring也集成EhCache!

说白了你还不懂得用,对EJB3.0的理解也太简单了,EJB事务有CMT/BMT,是否要自动Rollback看你的需求
3 楼 oak2008 2007-09-12  
看一下这个类,你们认为是数据是不是在内存里呢?我认为它就是在内存里,而不是缓存!
/*
* JCatalog Project
*/
package catalog.model.service.impl;

import java.util.List;
import java.util.LinkedList;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
//
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
//
import org.springframework.dao.DataIntegrityViolationException;
//
import catalog.model.service.CatalogService;
import catalog.model.exception.CatalogException;
import catalog.model.exception.DuplicateProductIdException;
import catalog.model.dao.CatalogDao;
import catalog.model.businessobject.Category;
import catalog.model.businessobject.Product;

/**
* The implementation of the <code>CatalogService</code>.
* <p>
* Spring Framework is used to manage this service bean.
* Since this class is not dependend on Spring API, it can be used outside the Spring IOC container.
* <p>
* Read/write caches for products and categories are implemented inside this class.
* It will be configured as a singleton in the Spring container.
* So the caches are in the application scope.
*
* @author <a href="mailto:derek_shen@hotmail.com">Derek Y. Shen</a>
* @see CatalogService
*/
public class CachedCatalogServiceImpl implements CatalogService {
//the logger for this class
private Log logger = LogFactory.getLog(this.getClass());

//the CatalogDao used
private CatalogDao catalogDao;

//the read/write cache for products
private Map productCache;

//the read/write cache for categories
private Map categoryCache;

/**
* Default constructor.
*
* @throws CatalogException If internal error occurs while populates the caches
*/
public CachedCatalogServiceImpl() throws CatalogException {
this.productCache = Collections.synchronizedMap(new LinkedHashMap());
this.categoryCache = Collections.synchronizedMap(new LinkedHashMap());
}

/**
* Set the <code>CatalogDao</code>.
* <p>
* It can be used by the Spring IOC container.
*
* @param newCatalogDao the CatalogDao to be set
*/
public void setCatalogDao(CatalogDao newCatalogDao) {
this.catalogDao = newCatalogDao;
}

/**
* @see CatalogService#saveProduct(Product)
*/
public Product saveProduct(Product product) throws CatalogException {
this.logger.debug(("entering method saveProduct"));

try {
Product newProduct = this.catalogDao.saveProduct(product);

this.productCache.put(newProduct.getId(), newProduct);

return newProduct;
} catch (DataIntegrityViolationException de) {
String msg = "Could not save product, duplicate product id " + de.getMessage();
this.logger.error(msg, de);

throw new DuplicateProductIdException(msg, de);
} catch (Exception e) {
String msg = "Could not save product " + e.toString();
this.logger.error(msg, e);

throw new CatalogException(msg, e);
}
}

/**
* @see CatalogService#updateProduct(Product)
*/
public void updateProduct(Product product) throws CatalogException {
this.logger.debug(("entering method updateProduct"));

try {
this.catalogDao.updateProduct(product);

this.productCache.put(product.getId(), product);
} catch (Exception e) {
String msg = "Could not update product " + e.getMessage();
this.logger.error(msg, e);

throw new CatalogException(msg, e);
}
}

/**
* @see CatalogService#deleteProduct(Product)
*/
public void deleteProduct(Product product) throws CatalogException {
this.logger.debug(("entering method deleteProduct"));

try {
this.catalogDao.deleteProduct(product);

this.productCache.remove(product.getId());
} catch (Exception e) {
String msg = "Could not delete product " + e.getMessage();
this.logger.error(msg, e);

throw new CatalogException(msg, e);
}
}

/**
* @see CatalogService#getProduct(String)
*/
public Product getProduct(String id) {
return (Product)this.productCache.get(id);
}

/**
* @see CatalogService#getCategory(String)
*/
public Category getCategory(String id) {
return (Category)this.categoryCache.get(id);
}

/**
* @see CatalogService#getAllProducts()
*/
public List getAllProducts() {
return this.getValueList(this.productCache);
}

public List getAllCategories() {
return this.getValueList(this.categoryCache);
}

public void init() throws CatalogException {
try {
this.populateCache();
} catch (Exception e) {
String msg = "Could not populate cache";
this.logger.error(msg, e);

throw new CatalogException(msg, e);
}
}

/**
* @see CatalogService#getAllCategories()
*/
private void populateCache() {
List products = this.catalogDao.getAllProducts();

for (int i=0; i<products.size(); i++) {
Product p = (Product)products.get(i);
this.productCache.put(p.getId(), p);
}

List categories = this.catalogDao.getAllCategories();

for (int i=0; i<categories.size(); i++) {
Category c = (Category)categories.get(i);
this.categoryCache.put(c.getId(), c);
}
}

private List getValueList(Map data) {
List list = new LinkedList();
Collection values = data.values();

Iterator ite = values.iterator();
while(ite.hasNext()) {
list.add(ite.next());
}

return list;
}
}
2 楼 差沙 2007-09-12  
没有推崇spring的意思,但是你的说的功能spring也都能“很好”的实现。当然EJB做的也应该很优秀,没有过多了解。同时希望楼主不要没有深入了解就否定一个优秀的框架,对自己也是损失。

另外,你说的什么新闻系统每个文章能达到1M呀,那要多少字呀。。。
而且这个新闻系统的每个文章都这么大,,,就算是小说网站也够呛呀。
而且,真的有这样的系统也不可能一次全都取出来,放进缓存。程序也是需要人来设计的。。
1 楼 惊鸿逝水 2007-09-12  
感觉你对spring的理解有点想当然?虽然我没看过PPut JSF to work
但demo毕竟只是demo不是生产环境下的应用,spring不至于没有cache,而把数据库的记录全部放在内存而不是cache中

你选择EJB3.0的理由也不够充分,也是在想当然耳!

相关推荐

    演示EJB3.0 + JPA + MySQL5.0 + C3P0连接池技术实战编程(Top-Down的XP开发方式)

    如果运行一切正常,那么你会看到使用EJB 3.0组件与JPA技术层技术完成的Hello world演示应用。 注意:配置JBoss服务器和调试的动作参见readme.txt文档,有详细说明怎样匹配连接池,以及可能遇到的问题及解决办法。该...

    cxf(jax-ws)+spring+hibernate整合包

    FastInfoset-1.2.12.jar,geronimo-javamail_1.4_spec-1.7.1.jar,geronimo-jaxws_2.2_spec-1.1.jar,geronimo-jms_1.1_spec-1.1.1.jar,geronimo-servlet_3.0_spec-1.0.jar,hibernate-annotations.jar,hibernate-...

    flex parsley IOC框架笔记

    flex actionscript 开发 对flex mxml技术的掌握 对java数据库后台框架 spring+hibernate ejb 3.0+jpa 等等后台技术

    spring 3.0 jar 所有开发包及开发项目实例

    spring 3.0 jar 最新所有开发包及开发项目实例 spring 3.0已经全面支持OSGi了。 各发行包的大致描述如下: org.springframework.asm-3.0.0.M4.jar: 提供对ASM(一个字节码框架)的简单封装 org.springframework....

    Hibernate实战(第2版) 中文版 part1

    本书是毋庸置疑的Hibernate和ORM(对象/关系映射)权威著作,由包括Hibernate之父在内的两位核心开发人员亲自执笔,详细讲述了Hibernate 3.2、Java Persistence和EJB 3.0标准。 本书通过一个应用将数百个例子融合...

    Hibernate实战(第2版) 中文版 part4

    本书是毋庸置疑的Hibernate和ORM(对象/关系映射)权威著作,由包括Hibernate之父在内的两位核心开发人员亲自执笔,详细讲述了Hibernate 3.2、Java Persistence和EJB 3.0标准。 本书通过一个应用将数百个例子融合...

    Hibernate实战(第2版) 中文版 part3

    本书是毋庸置疑的Hibernate和ORM(对象/关系映射)权威著作,由包括Hibernate之父在内的两位核心开发人员亲自执笔,详细讲述了Hibernate 3.2、Java Persistence和EJB 3.0标准。 本书通过一个应用将数百个例子融合...

    Hibernate实战(第2版) 中文版 part5

    本书是毋庸置疑的Hibernate和ORM(对象/关系映射)权威著作,由包括Hibernate之父在内的两位核心开发人员亲自执笔,详细讲述了Hibernate 3.2、Java Persistence和EJB 3.0标准。 本书通过一个应用将数百个例子融合...

    Hibernate实战(第2版) 中文版 part2

    本书是毋庸置疑的Hibernate和ORM(对象/关系映射)权威著作,由包括Hibernate之父在内的两位核心开发人员亲自执笔,详细讲述了Hibernate 3.2、Java Persistence和EJB 3.0标准。 本书通过一个应用将数百个例子融合...

    J2EE教学PPT课件.zip

    J2EE教学PPT课件 01 JavaEE介绍和环境配置.ppt 02 JDBC.ppt 03 JSP基础编程.ppt 04 JSP内置对象.ppt 05 JSP和JavaBean.ppt ...17 EJB3.0_会话Bean.ppt 18 EJB3.0_实体Bean.ppt 19 log4j和Ant.ppt 20 DOM和SAX.ppt

    林信良 笔记全集 struts hibernate spring ajax ibtatis

    struts hibernate spring ajax ibtatis java jsp/servlet ejb3.0 jsf junit 设计模式 等 非常不错,

    J2EE教程资料 JavaEE程序设计与应用开发教程 全套PPT课件资源集合 共21个章节 含上机习题和全部源代码.rar

    【完整课程列表】 01 JavaEE介绍和环境配置(共30页) 02 JDBC(共45页) ...17 EJB3.0_会话Bean(共29页) 18 EJB3.0_实体Bean(共25页) 19 log4j和Ant(共25页) 20 DOM和SAX(共33页) 上机习题(共5页)

    精通spring--源代码

    包括JNDI集成,EJB3.0集成.线程池和任务调度集成。Java消息服务集成,Java Mail集成,远程服务集成。Java管理扩展集成,Java EE连接器架构集成  专注于Spring2.5高级特性的研究。包括忘却的Spring高级话题,Spring...

    使用Annotation并对DAO层封装具有分页功能的S2SH整合实例_好资源0分送

    现在Annotation越来越流行,最近一段时间也学了一些,EJB3.0、Hibernate、Spring等都很好地支持Annotation,而且熟悉了Annotation的话整个项目开发时间会缩短,代码封装比较好,但是封装带来的就是代码阅读就比较...

    传智播客(黎活明ssh+ejb)

    传智播客黎活明老师讲课的PPT汇总,web框架Struts2,Spring2.5,Hibernate3.3,ejb3.0的PPT教程

    J2EE软件工程师全部培训课程总结(205页)

    内容包括:UML、ORALCE、JDBC、JSP、AJAX、SERVLET、JavaScript、Struts、JSF、Hibernate3.0、Spring、JMS、CVS、EJB3.0...看了之后,觉得这些核心技术资料的总结比自己的总结还细,本人觉得这套资料真的很不错,现上传...

    精通Spring(书签)

    包括JNDI集成,EJB3.0集成.线程池和任务调度集成。Java消息服务集成,Java Mail集成,远程服务集成。Java管理扩展集成,Java EE连接器架构集成  专注于Spring2.5高级特性的研究。包括忘却的Spring高级话题,Spring...

    精通Spring(书签版)

    包括JNDI集成,EJB3.0集成.线程池和任务调度集成。Java消息服务集成,Java Mail集成,远程服务集成。Java管理扩展集成,Java EE连接器架构集成  专注于Spring2.5高级特性的研究。包括忘却的Spring高级话题,Spring...

    精通Spring (书签版)

    包括JNDI集成,EJB3.0集成.线程池和任务调度集成。Java消息服务集成,Java Mail集成,远程服务集成。Java管理扩展集成,Java EE连接器架构集成  专注于Spring2.5高级特性的研究。包括忘却的Spring高级话题,Spring...

Global site tag (gtag.js) - Google Analytics