Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Purging old records using CMP  RSS feed

 
vivek srivastav
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am doing something like this to delete old records. I am not sure if it is the right approach. I am kind of worried about the finder returning too big a collection object. What should I do? :confused

/**
* @ejb.interface-method
* @param olderThan
* @throws CrystalException
*/
public void PurgeAudits(int olderThan) throws CrystalException {
try{
Context initial = new InitialContext();
Object objref = initial.lookup(AuditLocalHome.JNDI_NAME);
AuditLocalHome home = (AuditLocalHome)PortableRemoteObject.narrow(objref,AuditLocalHome.class);
long l = System.currentTimeMillis() - olderThan*24*60*60*1000;
SimpleDateFormat dfmt = new SimpleDateFormat("MM/dd/yy 00:00 'AM', z");
String strDate = dfmt.format(new Date(l));
java.util.Date d = dfmt.parse(strDate);
Collection c = home.findByOlderThanDate(d);
Iterator itor = c.iterator();
int i=0;
while(itor.hasNext()){
AuditLocal al = (AuditLocal)itor.next();
//Timestamp ts = (Timestamp)al.getPrimaryKey();
//if(ts.before(d)){
al.remove();
//}
}
} catch (Exception ex) {
ex.printStackTrace();
log("PurgeAudits: "
+ ex.getClass().getName()
+ " :"
+ ex.getMessage());
throw new CrystalException("PurgeAudits: "
+ ex.getClass().getName()
+ " :"
+ ex.getMessage());
}
}
 
Kunaal A Trehan
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I wud prefer getting datasource and then connection from the EJB.
Then I wud run the sql query instead of fetching a collection and deleting the records one by one.

Rgds
Kunal
 
Bruno Collet
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
To avoid manipulating, like you said, large collections, it would be wise to run the process at regular intervals. One way of doing this is to use the EJB Timer Service (provided you are in EJB 2.1). This service can automatically run an EJB (session) method. You should define a time interval so that the obsolete data to delete is not too big.
Hope it helps.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!