前言作者使用了EJB CMP2.0抽象持久性模式,结合会话Bean,验证JBoss EJB CMP2.0 O/R Mapping的数据处理能力。CMP/CMR能够从容应付各种表结构和关系的映射,CMP2.0对于数据查询也有一套EJB QL的机制。但就测试结果来看,JBoss CMP2.0还不足以应付大量的数据。
测试环境
操作 系统:Win2000
CPU:PIII733EB
Memory:512M
Application Server:JBoss-3.0.2
DBMS:SAP DB7.3
Java VM:JDK-1.4.1
Ant:1.5.1[@more@] 测试方案
*表结构
表:AUX_TYPE
COLUMN名 SAP DB类型 Java类型
AUX_ID (Primary Key) Integer Integer
AUX_DESC Varchar (50) String
表:TEST_NEWS
COLUMN名 SAP DB类型 Java类型
TID Integer Integer
AUX_ID (Foreign key ref erence to AUX_TYPE(AUX_ID)) Integer Integer
TNAME Varchar (30) String
TDESC Varchar (100) String
AMOUNT Integer Integer
*EJB结构
使用具有本地接口的EntityBean与表建立对应关系。使用一个无状态的SessionBean与客户端交互。SessionBean的作用是充当EntityBean的代理,并作为粗粒度的Bean提供远程数据访问。表AUX_TYPE主键的最大ID值也由SessionBean的(使用JDBC连接)方法提供。其他一些EJB QL不能实现,针对 数据库特殊的查询方法同样可以在SessionBean中实现。
*测试代码
…
private Category log = Category.getInstance(getClass());
private AuxTypeHome auxTypeHome = null;
private TestNewsHome testNewsHome = null;
private Context initial = null;
public void addNews(
Integer tId,
String tName,
String tDesc,
Integer amount,
String auxDesc) {
AuxType auxType = null;
try {
auxType = auxTypeHome.findByDesc(auxDesc);
log.info("@@@@@@@@" + auxType.getAuxDesc());
if (auxType == null)
throw new EJBException("AUX_TYPE为空!");
} catch (FinderException e) {
throw new EJBException("findAuxType:" + auxDesc + "出错!");
}
try {
TestNews testNews = testNewsHome.create(tId, tName, tDesc, amount);
testNews.setAuxType(auxType);
} catch (CreateException e) {
throw new EJBException(
"create TestNews:" + tId + "," + tName + "出错!");
}
}
private Connection connection = null;
private DataSource source = null;
public synchronized int getNextId() throws RemoteException, SQLException {
Statement stmt = null;
int id = -1;
try {
if (initial == null)
initial = new InitialContext();
if (source == null)
source = (DataSource) initial.lookup(" java:/SapdbDS");
if (connection == null)
connection = source.getConnection();
stmt = connection.createStatement();
String sql = "select max(TID) from TEST_NEWS";
ResultSet rs = stmt.executeQuery( sql);
if (rs.next()) {
id = rs.getInt(1);
if (id <= 0) {
id = 1;
}
} else
id = 0;
} catch (NamingException e) {
} finally {
try {
if (stmt != null)
stmt.close();
} catch (Exception e) {
}
}
return id + 1;
}
public Collection findNews(String find) {
Collection news = null;
try {
news = testNewsHome.findAll();
} catch (FinderException e) {
throw new EJBException("findNews:" + find + "出错!");
}
return news;
}
public void removeNews(Integer tId) {
try {
testNewsHome.remove(tId);
} catch (RemoveException e) {
throw new EJBException("removeNews:" + tId + " 出错!");
}
}
private void getHomes() throws NamingException {
if (initial == null)
initial = new InitialContext();
auxTypeHome = getAuxTypeHome(initial);
testNewsHome = getTestNewsHome(initial);
}
public void ejbCreate() throws CreateException {
try {
getHomes();
} catch (NamingException e) {
throw new CreateException("初始化SessionBean出错!");
}
}
/**
* @see javax.ejb.SessionBean#ejbActivate()
*/
public void ejbActivate() throws EJBException, RemoteException {
}
/**
* @see javax.ejb.SessionBean#ejbPassivate()
*/
public void ejbPassivate() throws EJBException, RemoteException {
}
/**
* @see javax.ejb.SessionBean#ejbRemove()
*/
public void ejbRemove() throws EJBException, RemoteException {
auxTypeHome = null;
testNewsHome = null;
initial = null;
source = null;
try {
if (connection != null)
connection.close();
} catch (Exception e) {
}
}
…
*测试流程
客户端 程序调用SessionBean的方法增加TEST_NEWS表的一行,TID值由ADO方法查询, 数据库连接在ejbRemove方法中才释放,减少连接所需要的时间。对于AUX_DESC,通过findByDesc方法查询,如果查询不为空则增加一行,查询结果赋给TEST_NEWS的对象。
测试结果
操作:从AUX_TYPE查找按AUX_DESC匹配查询AUX_TYPE,新增200个TEST_NEWS
费时:11276ms
操作:从AUX_TYPE查找按AUX_DESC匹配查询AUX_TYPE,用JDBC DataSource从TEST_NEWS表中查找最大TID,新增200个TEST_NEWS
费时:15993ms
费时:12738ms(优化JDBC Connection)
同时笔者测试了在数据量不断增长时 系统的性能,以下是结果:
插入行(插入前清空) 费时ms 费时ms (在已有行基础上插入10行) 内存占用,K
0 2214 22176
100 7200 1802 24452
200 12738 2894 34260
400 27499 2143 30600
600 50573 2704 33788
1000 134814 4226 51508
结论
当数据量倍增时,CMP表现出了接近成倍增加的资源消耗。这样的结果将最终表现出 系统对大数据量不适应,无法满足要求。当笔者试图测试4000行数据时,经过数十分钟漫长的等待,数据停留在3000多行,客户端报内存溢出的错误,这时的内存占用接近100M。
测试环境
操作 系统:Win2000
CPU:PIII733EB
Memory:512M
Application Server:JBoss-3.0.2
DBMS:SAP DB7.3
Java VM:JDK-1.4.1
Ant:1.5.1[@more@] 测试方案
*表结构
表:AUX_TYPE
COLUMN名 SAP DB类型 Java类型
AUX_ID (Primary Key) Integer Integer
AUX_DESC Varchar (50) String
表:TEST_NEWS
COLUMN名 SAP DB类型 Java类型
TID Integer Integer
AUX_ID (Foreign key ref erence to AUX_TYPE(AUX_ID)) Integer Integer
TNAME Varchar (30) String
TDESC Varchar (100) String
AMOUNT Integer Integer
*EJB结构
使用具有本地接口的EntityBean与表建立对应关系。使用一个无状态的SessionBean与客户端交互。SessionBean的作用是充当EntityBean的代理,并作为粗粒度的Bean提供远程数据访问。表AUX_TYPE主键的最大ID值也由SessionBean的(使用JDBC连接)方法提供。其他一些EJB QL不能实现,针对 数据库特殊的查询方法同样可以在SessionBean中实现。
*测试代码
…
private Category log = Category.getInstance(getClass());
private AuxTypeHome auxTypeHome = null;
private TestNewsHome testNewsHome = null;
private Context initial = null;
public void addNews(
Integer tId,
String tName,
String tDesc,
Integer amount,
String auxDesc) {
AuxType auxType = null;
try {
auxType = auxTypeHome.findByDesc(auxDesc);
log.info("@@@@@@@@" + auxType.getAuxDesc());
if (auxType == null)
throw new EJBException("AUX_TYPE为空!");
} catch (FinderException e) {
throw new EJBException("findAuxType:" + auxDesc + "出错!");
}
try {
TestNews testNews = testNewsHome.create(tId, tName, tDesc, amount);
testNews.setAuxType(auxType);
} catch (CreateException e) {
throw new EJBException(
"create TestNews:" + tId + "," + tName + "出错!");
}
}
private Connection connection = null;
private DataSource source = null;
public synchronized int getNextId() throws RemoteException, SQLException {
Statement stmt = null;
int id = -1;
try {
if (initial == null)
initial = new InitialContext();
if (source == null)
source = (DataSource) initial.lookup(" java:/SapdbDS");
if (connection == null)
connection = source.getConnection();
stmt = connection.createStatement();
String sql = "select max(TID) from TEST_NEWS";
ResultSet rs = stmt.executeQuery( sql);
if (rs.next()) {
id = rs.getInt(1);
if (id <= 0) {
id = 1;
}
} else
id = 0;
} catch (NamingException e) {
} finally {
try {
if (stmt != null)
stmt.close();
} catch (Exception e) {
}
}
return id + 1;
}
public Collection findNews(String find) {
Collection news = null;
try {
news = testNewsHome.findAll();
} catch (FinderException e) {
throw new EJBException("findNews:" + find + "出错!");
}
return news;
}
public void removeNews(Integer tId) {
try {
testNewsHome.remove(tId);
} catch (RemoveException e) {
throw new EJBException("removeNews:" + tId + " 出错!");
}
}
private void getHomes() throws NamingException {
if (initial == null)
initial = new InitialContext();
auxTypeHome = getAuxTypeHome(initial);
testNewsHome = getTestNewsHome(initial);
}
public void ejbCreate() throws CreateException {
try {
getHomes();
} catch (NamingException e) {
throw new CreateException("初始化SessionBean出错!");
}
}
/**
* @see javax.ejb.SessionBean#ejbActivate()
*/
public void ejbActivate() throws EJBException, RemoteException {
}
/**
* @see javax.ejb.SessionBean#ejbPassivate()
*/
public void ejbPassivate() throws EJBException, RemoteException {
}
/**
* @see javax.ejb.SessionBean#ejbRemove()
*/
public void ejbRemove() throws EJBException, RemoteException {
auxTypeHome = null;
testNewsHome = null;
initial = null;
source = null;
try {
if (connection != null)
connection.close();
} catch (Exception e) {
}
}
…
*测试流程
客户端 程序调用SessionBean的方法增加TEST_NEWS表的一行,TID值由ADO方法查询, 数据库连接在ejbRemove方法中才释放,减少连接所需要的时间。对于AUX_DESC,通过findByDesc方法查询,如果查询不为空则增加一行,查询结果赋给TEST_NEWS的对象。
测试结果
操作:从AUX_TYPE查找按AUX_DESC匹配查询AUX_TYPE,新增200个TEST_NEWS
费时:11276ms
操作:从AUX_TYPE查找按AUX_DESC匹配查询AUX_TYPE,用JDBC DataSource从TEST_NEWS表中查找最大TID,新增200个TEST_NEWS
费时:15993ms
费时:12738ms(优化JDBC Connection)
同时笔者测试了在数据量不断增长时 系统的性能,以下是结果:
插入行(插入前清空) 费时ms 费时ms (在已有行基础上插入10行) 内存占用,K
0 2214 22176
100 7200 1802 24452
200 12738 2894 34260
400 27499 2143 30600
600 50573 2704 33788
1000 134814 4226 51508
结论
当数据量倍增时,CMP表现出了接近成倍增加的资源消耗。这样的结果将最终表现出 系统对大数据量不适应,无法满足要求。当笔者试图测试4000行数据时,经过数十分钟漫长的等待,数据停留在3000多行,客户端报内存溢出的错误,这时的内存占用接近100M。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/802415/viewspace-825894/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/802415/viewspace-825894/