用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生命周期的介绍!
用ejb3.0即使web层和service层在同一机器,调用的时候也是要通过socket通信的,并发量大的时候,效率非常低,不知道这个问题你考虑过没有?
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
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:
This is vital for complex enterprise applications.But for small,isolated,dedicated system,it's not a big deal.
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.
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.
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 = "BILLING_DESCRIPTION", nullable = false) private String billingDescription;
@JoinColumn(name = "SERVICE", referencedColumnName = "ID") @ManyToOne private Service service=new Service();
}
...
@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;
em.persist(product); em.flush(); // save into database
... }
如果EJB3能成大气,能发展足够的用户量,Glassfish+Netbeans+EJB3这样的傻瓜套餐的开发速度的确比Spring+Hibernate快一些,说起来也规范些。就怕它跑到半路跪倒了,还有其他一些唯Spring是举的OSS对EJB3的支持怎样。
新闻系统?用啥ejb和hibernate,既然文字都有一M,还不如就用FreeMarker生成静态文本呢。
新闻系统又不是啥事务敏感系统,用得着EJB或者Spring AOP么?
真是瞎胡闹。
EJB3.0我并没有选择Jboss的那个hibernate实现版,我选择的是sun公司提倡的TopLink的标准版,服务器我也不用jboss,它已经收费了,我选择的是glassfish!
EJB3,关注了很久,但偶只会用而已,为什么,因为Spring的代码很明确在Spring提供的包里,而EJB的呢?JBOSS里找,JBoss的代码很乱乱,我不喜欢,至于EJB3还是Spring的功能,呵呵,毕竟都是框架而已,功能有没有是另外一回事,偶觉得框架在开发过程只是决定了你的开发方式,至于功能是你自己的事情
用EJB3.0访问很多记录,我的测试里是3000记录,我昨晚已经做过测试,性能还是很好,500ms左右.用以前设计的Cached(Spring),用时间大概是70ms!这个差距不到一秒,考虑到上的原因,用EJB3.0还是合算的!
这个是存在map里面,又能说明什么呢?这就能说明: “Spring的业务层的缓冲类要自己设计,设计是有些复杂的”了?
ejb3压根没提二级缓存,一级缓存与hibernate相同。 ejb3持久那块不是不好,只是没有hibernate包含的多
都说了是Demo了,自己写个基于LRU Cache也很简单,再说spring也集成EhCache!
说白了你还不懂得用,对EJB3.0的理解也太简单了,EJB事务有CMT/BMT,是否要自动Rollback看你的需求
看一下这个类,你们认为是数据是不是在内存里呢?我认为它就是在内存里,而不是缓存! /*
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 CatalogService.
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.
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 Derek Y. Shen
@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;
/**
/**
/**
@see CatalogService#saveProduct(Product) */ public Product saveProduct(Product product) throws CatalogException { this.logger.debug(("entering method saveProduct"));
try { Product newProduct = this.catalogDao.saveProduct(product);
} catch (DataIntegrityViolationException de) { String msg = "Could not save product, duplicate product id " + de.getMessage(); this.logger.error(msg, de);
} catch (Exception e) { String msg = "Could not save product " + e.toString(); this.logger.error(msg, e);
} }
/**
@see CatalogService#updateProduct(Product) */ public void updateProduct(Product product) throws CatalogException { this.logger.debug(("entering method updateProduct"));
try { this.catalogDao.updateProduct(product);
} catch (Exception e) { String msg = "Could not update product " + e.getMessage(); this.logger.error(msg, e);
} }
/**
@see CatalogService#deleteProduct(Product) */ public void deleteProduct(Product product) throws CatalogException { this.logger.debug(("entering method deleteProduct"));
try { this.catalogDao.deleteProduct(product);
} catch (Exception e) { String msg = "Could not delete product " + e.getMessage(); this.logger.error(msg, e);
} }
/**
/**
/**
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);
}
/**
@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();
} }
没有推崇spring的意思,但是你的说的功能spring也都能“很好”的实现。当然EJB做的也应该很优秀,没有过多了解。同时希望楼主不要没有深入了解就否定一个优秀的框架,对自己也是损失。
另外,你说的什么新闻系统每个文章能达到1M呀,那要多少字呀。。。 而且这个新闻系统的每个文章都这么大,,,就算是小说网站也够呛呀。 而且,真的有这样的系统也不可能一次全都取出来,放进缓存。程序也是需要人来设计的。。
感觉你对spring的理解有点想当然?虽然我没看过PPut JSF to work 但demo毕竟只是demo不是生产环境下的应用,spring不至于没有cache,而把数据库的记录全部放在内存而不是cache中
你选择EJB3.0的理由也不够充分,也是在想当然耳!