我认为在你使用ejb home接口调用findByPrimaryKey(XXXKey key)(或create(...)插入数据库成功返回XXXKey时)时这个 context 和具体的数据库记录对应的 ejb 实例关联的。
关于:context.getPrimaryKey() :他需要你自己代码,一般代码向下面这段代码,j2ee的容器根据你写的ejbFindByPrimaryKey(ShipKey primaryKey) 方法来生成查询对应数据库表对应纪录,并以该数据库表的关键字段作为实体bean的关键字的一个特定字段的特定值。
public ShipKey ejbFindByPrimaryKey(ShipKey primaryKey) throws FinderException{
Connection con=null;
PreparedStatement ps=null;
ResultSet result=null;
try{
con=this.getConnection();
ps=con.prepareStatement("select id from Ship where id=?");
ps.setInt(1,primaryKey.id);//id是Ship表中关键字断
result=ps.executeQuery();
if(!result.next()){
throw new ObjectNotFoundException("Cannot find Ship with id= "+id);
}
}
catch(SQLException se){
throw new EJBException(se);
}
finally{
try{
if(result!=null)
result.close();
if(ps!=null)
ps.close();
if(con!=null)
con.close();
}
catch(SQLException se){
se.printStackTrace();
}
}
return primaryKey;
}其中getConnection()
public Connection getConnection(){
Connection con=null;
try{
Context context=new InitialContext();
//Connection con=null;
DataSource source=(DataSource)context.lookup("ORDR");
con=source.getConnection(); }
catch(Exception e){
throw new javax.ejb.EJBException(e.toString());
}
return con;
}在容器调用ejbLoad()和ejbRemove()方法时它会根据关键字对象中对应数据库表的关键字段值来对数据库表中相对纪录进行操作的。
对于连接问题:在实体bean对象创建之前连接池已经预先创建了几个(一般小于最大设值数)(这个数据可以自己设定)数据库连接。一般写实体bean中操作方法什么时候用到连接就调用getConnection()这个方法(它从连接池中闲置的数据库连接随机取得一个),用完就立即关闭数据库连接(con.close();实际上时关闭于连接池的连接)。如果不关闭数据库服务器很快就会被搞死的!
关于:context.getPrimaryKey() :他需要你自己代码,一般代码向下面这段代码,j2ee的容器根据你写的ejbFindByPrimaryKey(ShipKey primaryKey) 方法来生成查询对应数据库表对应纪录,并以该数据库表的关键字段作为实体bean的关键字的一个特定字段的特定值。
public ShipKey ejbFindByPrimaryKey(ShipKey primaryKey) throws FinderException{
Connection con=null;
PreparedStatement ps=null;
ResultSet result=null;
try{
con=this.getConnection();
ps=con.prepareStatement("select id from Ship where id=?");
ps.setInt(1,primaryKey.id);//id是Ship表中关键字断
result=ps.executeQuery();
if(!result.next()){
throw new ObjectNotFoundException("Cannot find Ship with id= "+id);
}
}
catch(SQLException se){
throw new EJBException(se);
}
finally{
try{
if(result!=null)
result.close();
if(ps!=null)
ps.close();
if(con!=null)
con.close();
}
catch(SQLException se){
se.printStackTrace();
}
}
return primaryKey;
}其中getConnection()
public Connection getConnection(){
Connection con=null;
try{
Context context=new InitialContext();
//Connection con=null;
DataSource source=(DataSource)context.lookup("ORDR");
con=source.getConnection(); }
catch(Exception e){
throw new javax.ejb.EJBException(e.toString());
}
return con;
}在容器调用ejbLoad()和ejbRemove()方法时它会根据关键字对象中对应数据库表的关键字段值来对数据库表中相对纪录进行操作的。
对于连接问题:在实体bean对象创建之前连接池已经预先创建了几个(一般小于最大设值数)(这个数据可以自己设定)数据库连接。一般写实体bean中操作方法什么时候用到连接就调用getConnection()这个方法(它从连接池中闲置的数据库连接随机取得一个),用完就立即关闭数据库连接(con.close();实际上时关闭于连接池的连接)。如果不关闭数据库服务器很快就会被搞死的!
解决方案 »
- 我们爱分享----一些工作上需要注意的小事情..
- 【求助】java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor
- strut2前台页面实现for循环
- getJdbcTemplate 获取插入数据的行号
- Weblogic9中文显示问题(AppFuse的sitemesh引起)有人遇到过吗?解决200分伺候着!
- 关于CMP的级联删除,高手请进
- jbuilder+weblogic的一个session bean客户端lookup时报错
- 100分深情大奉送!! 请教各位高手帮忙
- 关于J2EE的就业前景,值不值的花二万元参加一个二十天的培训,欢迎大家热烈讨论
- 请问java分别赋值abcd四个整数排序怎么排呢?
- 请教一个struts问题 请高手帮忙!
- 再没人帮我我要吐血了
1、但是你的观点毕竟只是感性的认为,要是有什么确切的证据表名 Context 是在 findPrimaryKey or create 后就和特定的 ejb 关联就好了?还有你说“context.getPrimaryKey() :他需要你自己代码,一般代码向下面这段代码,j2ee的容器根据你写的ejbFindByPrimaryKey(ShipKey primaryKey) 方法来生成查询对应数据库表对应纪录,并以该数据库表的关键字段作为实体bean的关键字的一个特定字段的特定值。”
我觉得不对,context.getPrimaryKey() 本身就是取得关键字,如果关键字本身都未取到,如何从数据库中找到相关的信息呢?context.getPrimaryKey() 的调用时机就是不能确定主键的时候而获得组件来进行后面的操作。2、其实我自己也认为 getConnection()应该在需要的时候调用并在不用的时候关闭,但是sun 的ejb指南中的很多例子却不是这样做的,(我觉得很纳闷)。另外,如果按照“即用即关”的方法,那还是有几个值得考虑的地方,首先是通过 jndi 获取 dataSource,然后通过dataSource.getConnection() 获取 Connection,这个过程的开销大不大?(主要是时间的开销,特别的是 ejb容器 和 命名目录服务器 不在同一个服务器内的情况)