Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Question about ejbActivate

3 views
Skip to first unread message

Jeffrey Spoon

unread,
Feb 12, 2006, 12:18:40 PM2/12/06
to

Hello. I have just started with J2EE and have come across a problem with
one of my entity beans.

Basically, I'm not sure how to use a finder method which has a different
object to the findByPrimaryKey method.

My bean ejbActivate method is:

public void ejbActivate() {

//String numberString = (String) context.getPrimaryKey();
//historyId = new Integer(numberString);
historyId = (Integer) context.getPrimaryKey();

}

My findByPrimaryKey method returns an Integer object. This works fine,
but I have tried to implement a findByShare method, which returns a
String.

When the client runs the business methods associated with the
findByShare instance, it throws a java.lang.String class cast exception.
If I change ejbactivate to the commented out lines (and comment out the
integer cast) then findByPrimaryKey won't work and throws and Integer
class cast exception.

How do I get around this? And why is the String cast occurring when it
isn't looking for the primary key? I'm lost there...

Here is the full code and stack traces:


---------------------

ShareHistory Entity Bean class:

import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;


public class ShareHistoryBean implements EntityBean {

private static final String dbName = "java:comp/env/jdbc/politse";
private Integer historyId; //PK
private String shareId; //FK *no constraint*
private int value; //value of share in pence
private String time; //time share price changed
private String date; //date share price changed

private Connection con;
private EntityContext context;

/*********************** Business Routines
*************************/


public String getShareId(){
return shareId;
}//getshareid

public String getTime(){
return time;
}//gettime

public String getDate(){
return date;
}//getdate

public int getValue() {
return value;
}//getvalue


/************************************************/


public Integer ejbCreate(Integer historyId, String shareId, int
value, String time, String date)
throws CreateException {
System.out.println("in ejbCreate");

try {
logShare(historyId, shareId, value, time, date);
} catch (Exception ex) {
throw new EJBException("ejbCreate: " + ex.getMessage());
}

this.historyId = historyId;
this.shareId = shareId;
this.value = value;
this.time = time;
this.date = date;

System.out.println("about to leave ejbCreate");

return historyId;
}//ejbcreate


public Integer ejbFindByPrimaryKey(Integer primaryKey) throws
FinderException {
boolean result;

try {
result = selectByPrimaryKey(primaryKey);
} catch (Exception ex) {
throw new EJBException("ejbFindByPrimaryKey: " +
ex.getMessage());
}

if (result) {
return primaryKey;
} else {
throw new ObjectNotFoundException("Row for id " + primaryKey
+
" not found.");
}

}//ejbfindbyprimarykey


public Collection ejbFindByShare(String shareId) throws
FinderException {

Collection result;

try {
result = selectByShare(shareId);
} catch (Exception ex) {
throw new EJBException("ejbFindByShare " + ex.getMessage());
}

return result;

}//findbyshare

public Collection ejbFindByDateTime(String date, String time)
throws FinderException {
Collection result;

try {
result = selectByDateTime(date, time);
} catch (Exception ex) {
throw new EJBException("ejbFindByDateTime " +
ex.getMessage());
}

return result;

}//findbydatetime

public void ejbRemove() {

try {
deleteLog(historyId);
} catch (Exception ex) {
throw new EJBException("ejbRemove: " + ex.getMessage());
}
}//ejbremove


public void setEntityContext(EntityContext context) {
this.context = context;
}//setentitycontext


public void unsetEntityContext() {
}//unsetentitycontext

public void ejbActivate() {

//String numberString = (String) context.getPrimaryKey();
//historyId = new Integer(numberString);
historyId = (Integer) context.getPrimaryKey();

}

public void ejbPassivate() {
historyId = null;
}

public void ejbLoad() {

System.out.println("in ejbLoad");

try {
loadLog();
} catch (Exception ex) {
throw new EJBException("ejbLoad: " + ex.getMessage());
}

System.out.println("leaving ejbLoad");

}//ejbload

public void ejbStore() {
System.out.println("in ejbStore");

try {
storeLog();
} catch (Exception ex) {
throw new EJBException("ejbStore: " + ex.getMessage());
}

System.out.println("leaving ejbStore");
}//ejbstore

public void ejbPostCreate(Integer historyId, String shareId, int
value, String time, String date) {

}//ejbpostcreate


/*********************** Database Routines
*************************/

private void makeConnection() {
try {
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(dbName);

con = ds.getConnection();
} catch (Exception ex) {
throw new EJBException("Unable to connect to database. " +
ex.getMessage());
}
}//makeconnection


private void releaseConnection() {
try {
con.close();
} catch (SQLException ex) {
throw new EJBException("releaseConnection: " +
ex.getMessage());
}
}//releaseconnection


private boolean selectByPrimaryKey(Integer primaryKey)
throws SQLException {
makeConnection();

String selectStatement =
"select shareid " + "from sharehistory where historyid = ?
";
PreparedStatement prepStmt =
con.prepareStatement(selectStatement);

prepStmt.setInt(1, primaryKey);

ResultSet rs = prepStmt.executeQuery();
boolean result = rs.next();

prepStmt.close();
releaseConnection();

return result;

}//selectbyprimarykey

private Collection selectByShare(String shareId) throws SQLException
{

makeConnection();

String selectStatement =
"select shareid " + "from sharehistory where shareid = ? ";
PreparedStatement prepStmt =
con.prepareStatement(selectStatement);

prepStmt.setString(1, shareId);

ResultSet rs = prepStmt.executeQuery();
ArrayList a = new ArrayList();

while (rs.next()) {
String id = rs.getString(1);

a.add(id);
}

prepStmt.close();
releaseConnection();

return a;
}//selectbyshare

private Collection selectByDateTime(String date, String time) throws
SQLException {

makeConnection();

String selectStatement =
"select * " + "from sharehistory where date = ?, time = ? ";
PreparedStatement prepStmt =
con.prepareStatement(selectStatement);

prepStmt.setString(1, date);
prepStmt.setString(2, time);

ResultSet rs = prepStmt.executeQuery();
ArrayList a = new ArrayList();

while (rs.next()) {
String id = rs.getString(1);//shareid
Integer in = rs.getInt(2);//value
String ti = rs.getString(3);//time
String da = rs.getString(4);//date

a.add(id);
a.add(in);
a.add(ti);
a.add(da);
}

prepStmt.close();
releaseConnection();

return a;

}//selectbydatetime


private void deleteLog(int historyId) throws SQLException {

makeConnection();

String deleteStatement =
"delete from sharehistory " + "where historyid = ?";
PreparedStatement prepStmt =
con.prepareStatement(deleteStatement);

prepStmt.setInt(1, historyId);
prepStmt.executeUpdate();
prepStmt.close();
releaseConnection();
}


private void loadLog() throws SQLException {

makeConnection();

String selectStatement =
"select historyid, shareid, value, time, date " + "from
sharehistory where historyid = ? ";
PreparedStatement prepStmt =
con.prepareStatement(selectStatement);

prepStmt.setInt(1, historyId);

ResultSet rs = prepStmt.executeQuery();

if (rs.next()) {

historyId = rs.getInt(1);
shareId = rs.getString(2);
value = rs.getInt(3);
time = rs.getString(4);
date = rs.getString(5);
prepStmt.close();

} else {
prepStmt.close();
throw new NoSuchEntityException("Row for shareId " + shareId
+
" not found in database.");
}

releaseConnection();
}//loadshares

private void storeLog() throws SQLException {

makeConnection();

String updateStatement =
"update sharehistory set shareid = ? ," +
"value = ? , time = ?, date = ? " + "where historyId = ?";
PreparedStatement prepStmt =
con.prepareStatement(updateStatement);

prepStmt.setString(1, shareId);
prepStmt.setInt(2, value);
prepStmt.setString(3, time);
prepStmt.setString(4, date);
prepStmt.setInt(5, historyId);

int rowCount = prepStmt.executeUpdate();

prepStmt.close();

if (rowCount == 0) {
throw new EJBException("Storing row for historyId " +
historyId +
" failed.");
}

releaseConnection();
}//storeshare


private void logShare(Integer historyId, String shareId, int value,
String time, String date)
throws SQLException {

makeConnection();
System.out.println("in logShare");

String insertStatement = "insert into sharehistory values ( ? ,
? , ? , ? , ? )";
PreparedStatement prepStmt =
con.prepareStatement(insertStatement);

prepStmt.setInt(1, historyId);
prepStmt.setString(1, shareId);
prepStmt.setInt(2, value);
prepStmt.setString(3, time);
prepStmt.setString(4, date);

prepStmt.executeUpdate();
prepStmt.close();
System.out.println("leaving logShare");
releaseConnection();
}//insertshare


}//class

-------------------------

ShareHistory Remote Home interface:

import java.util.*;
import java.rmi.RemoteException;
import javax.ejb.*;


public interface ShareHistoryHome extends EJBHome {

public ShareHistory create(Integer historyId, String shareId, int
value, String time, String date)
throws RemoteException, CreateException;

public ShareHistory findByPrimaryKey(Integer historyId)
throws FinderException, RemoteException;

public Collection findByShare(String shareId)
throws FinderException, RemoteException;

public Collection findByDateTime (String date, String time)
throws FinderException, RemoteException;
}

---------------------------------------------------

ShareHistory Home Interface:

import javax.ejb.EJBObject;
import java.rmi.RemoteException;
import java.util.*;


public interface ShareHistory extends EJBObject {

public int getValue() throws RemoteException;

public String getShareId() throws RemoteException;

public String getDate() throws RemoteException;

public String getTime() throws RemoteException;

}

----------------------------------------

ShareHistoryClient:

import java.util.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;


public class ShareHistoryClient {

public static void main(String[] args) {
try {
Context initial = new InitialContext();

Object objref =
initial.lookup("java:comp/env/ejb/SimpleShareHistory");
ShareHistoryHome sharesHistoryHome = (ShareHistoryHome)
PortableRemoteObject.narrow(objref, ShareHistoryHome.class);


Integer integ = new Integer(1);
ShareHistory his =
sharesHistoryHome.findByPrimaryKey(integ);
//Shares lovelyShares = sharesHome.create("DCAN", 600,
0, 0 );
//ShareHistory hers =
sharesHistoryHome.findByShare("DCAN");
System.out.println("shareid" + ": " +
his.getShareId());
System.out.println("value" + ": " +
his.getValue());
System.out.println("time" + ": " +
his.getTime());
System.out.println("date" + ": " +
his.getDate());

Collection c = sharesHistoryHome.findByShare("DCAN");

System.out.println("AFTER COLLECTION c");

Iterator i = c.iterator();

System.out.println("AFTER ITERATOR");

while (i.hasNext()) {
ShareHistory sh = (ShareHistory) i.next();
System.out.println("AFTER SHAREHISTORY SH =
SHAREHISTORY i.next");

//String id = (Integer) sh.getPrimaryKey();
System.out.println("AFTER SH.GETPRIMARYKEY");

System.out.println("shareid" + ": " + sh.getShareId());
System.out.println("value" + ": " + sh.getValue());
System.out.println("time" + ": " + sh.getTime());
System.out.println("date" + ": " + sh.getDate());

}//while

/* while (i.hasNext()) {
SavingsAccount account = (SavingsAccount) i.next();
String id = (String) account.getPrimaryKey();
BigDecimal amount = account.getBalance();

System.out.println(id + ": " + amount);
}//while
*/

} catch (Exception ex) {

System.err.println("Caught an exception.");
ex.printStackTrace();}

}//main

}//class

------------------------------------

The server stack trace:

nested exception is: java.lang.ClassCastException: java.lang.String
java.lang.ClassCastException: java.lang.String
at ShareHistoryBean.ejbActivate(ShareHistoryBean.java:134)
at
com.sun.ejb.containers.EntityContainer.activateEJBFromPool(EntityContaine
r.java:1943)
at
com.sun.ejb.containers.EntityContainer.getReadyEJB(EntityContainer.java:2
628)
at
com.sun.ejb.containers.EntityContainer._getContext(EntityContainer.java:5
04)
at
com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:1072)
at
com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:772)
at
com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocat
ionHandler.java:149)
at $Proxy80.getShareId(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java
:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorI
mpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at
com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTi
e.java:123)
at
com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatchT
oServant(CorbaServerRequestDispatcherImpl.java:648)
at
com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatch(
CorbaServerRequestDispatcherImpl.java:192)
at
com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequestRequ
est(CorbaMessageMediatorImpl.java:1709)
at
com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(Cor
baMessageMediatorImpl.java:1569)
at
com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleInput(Corba
MessageMediatorImpl.java:951)
at
com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback
(RequestMessage_1_2.java:181)
at
com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(Cor
baMessageMediatorImpl.java:721)
at
com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.dispatch(So
cketOrChannelConnectionImpl.java:469)
at
com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.doWork(Sock
etOrChannelConnectionImpl.java:1258)
at
com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.run(
ThreadPoolImpl.java:409)
javax.ejb.EJBException: nested exception is:
java.lang.ClassCastException: java.lang.String
at
com.sun.ejb.containers.EntityContainer.activateEJBFromPool(EntityContaine
r.java:1952)
at
com.sun.ejb.containers.EntityContainer.getReadyEJB(EntityContainer.java:2
628)
at
com.sun.ejb.containers.EntityContainer._getContext(EntityContainer.java:5
04)
at
com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:1072)
at
com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:772)
at
com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocat
ionHandler.java:149)
at $Proxy80.getShareId(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java
:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorI
mpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at
com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTi
e.java:123)
at
com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatchT
oServant(CorbaServerRequestDispatcherImpl.java:648)
at
com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatch(
CorbaServerRequestDispatcherImpl.java:192)
at
com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequestRequ
est(CorbaMessageMediatorImpl.java:1709)
at
com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(Cor
baMessageMediatorImpl.java:1569)
at
com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleInput(Corba
MessageMediatorImpl.java:951)
at
com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback
(RequestMessage_1_2.java:181)
at
com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(Cor
baMessageMediatorImpl.java:721)
at
com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.dispatch(So
cketOrChannelConnectionImpl.java:469)
at
com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.doWork(Sock
etOrChannelConnectionImpl.java:1258)
at
com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.run(
ThreadPoolImpl.java:409)

--------------------------------------------------------

And the client stack trace:


C:\EARS>appclient -client PolitseClient.jar -mainclass ShareHistoryClie
nt
shareid: DCAN
value: 400
time: 20:30:00
date: 2006-02-02
AFTER COLLECTION c
AFTER ITERATOR
AFTER SHAREHISTORY SH = SHAREHISTORY i.next
AFTER SH.GETPRIMARYKEY
Caught an exception.
java.rmi.ServerException: RemoteException occurred in server thread;
nested exce
ption is:
java.rmi.RemoteException: nested exception is:
java.lang.ClassCastExcept
ion: java.lang.String; nested exception is:
java.lang.ClassCastException: java.lang.String
at
com.sun.corba.ee.impl.javax.rmi.CORBA.Util.mapSystemException(Util.ja
va:161)
at javax.rmi.CORBA.Util.mapSystemException(Util.java:67)
at
com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.invo
ke(StubInvocationHandlerImpl.java:142)
at
com.sun.corba.ee.impl.presentation.rmi.bcel.BCELStubBase.invoke(Unkno
wn Source)
at
_ShareHistory_DynamicStub.getShareId(_ShareHistory_DynamicStub.java)
at ShareHistoryClient.main(ShareHistoryClient.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at
com.sun.enterprise.util.Utility.invokeApplicationMain(Utility.java:23
7)
at com.sun.enterprise.appclient.Main.<init>(Main.java:430)
at com.sun.enterprise.appclient.Main.main(Main.java:99)
Caused by: java.rmi.RemoteException: nested exception is:
java.lang.ClassCastExc
eption: java.lang.String; nested exception is:
java.lang.ClassCastException: java.lang.String
at
com.sun.enterprise.iiop.POAProtocolMgr.mapException(POAProtocolMgr.ja
va:199)
at
com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:85
3)
at
com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInv
ocationHandler.java:160)
at $Proxy84.getShareId(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at
com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(Reflecti
veTie.java:123)
at
com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispa
tchToServant(CorbaServerRequestDispatcherImpl.java:648)
at
com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispa
tch(CorbaServerRequestDispatcherImpl.java:192)
at
com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest
Request(CorbaMessageMediatorImpl.java:1709)
at
com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest
(CorbaMessageMediatorImpl.java:1569)
at
com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleInput(C
orbaMessageMediatorImpl.java:951)
at
com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.call
back(RequestMessage_1_2.java:181)
at
com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest
(CorbaMessageMediatorImpl.java:721)
at
com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.dispatc
h(SocketOrChannelConnectionImpl.java:469)
at
com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.doWork(
SocketOrChannelConnectionImpl.java:1258)
at
com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.
run(ThreadPoolImpl.java:409)
Caused by: java.lang.ClassCastException: java.lang.String
at ShareHistoryBean.ejbActivate(ShareHistoryBean.java:134)
at
com.sun.ejb.containers.EntityContainer.activateEJBFromPool(EntityCont
ainer.java:1943)
at
com.sun.ejb.containers.EntityContainer.getReadyEJB(EntityContainer.ja
va:2628)
at
com.sun.ejb.containers.EntityContainer._getContext(EntityContainer.ja
va:504)
at
com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:10
72)
at
com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:772
)
at
com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInv
ocationHandler.java:149)
... 16 more

--
Jeffrey Spoon

Doug Pardee

unread,
Feb 14, 2006, 7:48:38 PM2/14/06
to
What type is your Primary Key (PK) for this EJB? The FindByPrimaryKey
method says that it's an Integer. Some of your other finders seem to
think that it's a String. It can only be one type, and that's the type
that you specified in the prim-key-class field of your deployment
descriptor.

I think that you're confused about what the other finders in an Entity
EJB are supposed to do. They are supposed to return the PK, or a
Collection of PKs, of the entities (database records) that meet the
finder criteria. If your PK is an Integer associated with the
history_id field, then the other finders must return a [Collection of]
history_id value(s) defined as Integer(s).

The container (EJB server) will then take that PK (or those PKs) and
instantiate EJB(s) for them, and will return the EJB(s) to the caller -
possibly via remote proxies.

You shouldn't need to fool with "historyId" in your ejbActivate method.
If your bean instance gets passivated, the container will save that
value and will restore it before calling ejbActivate. What you DO need
to do is to release your Connection in ejbPassivate and get a new one
in ejbActivate, because a Connection is not Serializable. Also, you
should make your Connection variable "con" be "transient" so that the
container won't try to serialize it during passivation. Don't worry
about the EntityContext, though - the container knows how to deal with
that when passivating and activating.

Jeffrey Spoon

unread,
Feb 15, 2006, 3:53:16 PM2/15/06
to
In message <1139964518.1...@g44g2000cwa.googlegroups.com>, Doug
Pardee <dougp...@yahoo.com> writes

>What type is your Primary Key (PK) for this EJB? The FindByPrimaryKey
>method says that it's an Integer. Some of your other finders seem to
>think that it's a String. It can only be one type, and that's the type
>that you specified in the prim-key-class field of your deployment
>descriptor.
>
>I think that you're confused about what the other finders in an Entity
>EJB are supposed to do. They are supposed to return the PK, or a
>Collection of PKs, of the entities (database records) that meet the
>finder criteria. If your PK is an Integer associated with the
>history_id field, then the other finders must return a [Collection of]
>history_id value(s) defined as Integer(s).
>
>The container (EJB server) will then take that PK (or those PKs) and
>instantiate EJB(s) for them, and will return the EJB(s) to the caller -
>possibly via remote proxies.
>

Yep, I fixed it the other day. Like you say, I was returning the String
shareId instead of the Integer primary key, hence the String class cast
exception in ejbActivate. I realised this after re-reading the J2EE
manual that it states you must return a collection of primary keys in
the finder methods. You're right, I was a confused as to what they did.

>You shouldn't need to fool with "historyId" in your ejbActivate method.
>If your bean instance gets passivated, the container will save that
>value and will restore it before calling ejbActivate. What you DO need
>to do is to release your Connection in ejbPassivate and get a new one
>in ejbActivate, because a Connection is not Serializable. Also, you
>should make your Connection variable "con" be "transient" so that the
>container won't try to serialize it during passivation. Don't worry
>about the EntityContext, though - the container knows how to deal with
>that when passivating and activating.
>

Ok, I basically just copied an example ejbActivate from the J2EE manual,
and it assigned the primary key in ejbActivate.

I see what you mean about creating and releasing the connections in the
activate and passivate methods, but I'm not sure what you mean by making
the connection variable transient (well I understand the idea, but I'm
not sure how you'd implement it).

Thanks for wading through all that code, I didn't think anyone would
answer. Cheers.


--
Jeffrey Spoon

Doug Pardee

unread,
Feb 15, 2006, 4:45:18 PM2/15/06
to
> I'm not sure what you mean by making the connection variable transient
> (well I understand the idea, but I'm not sure how you'd implement it).

You add the "transient" keyword:
private transient Connection con;

Jeffrey Spoon

unread,
Feb 16, 2006, 4:16:16 PM2/16/06
to
In message <1140039918.1...@g47g2000cwa.googlegroups.com>, Doug
Pardee <dougp...@yahoo.com> writes

Ah right. Never heard of it until now. Thanks.

--
Jeffrey Spoon

0 new messages