mardi 1 avril 2014

JPA 2.0 Deadlock and DuplicateKeyException


Vote count:

0




Recently we have upgraded our application which was running on Weblogic 10 and using JPA 1.0. After the upgrade we started experiencing JPA exceptions while inserting and updating the database. These exceptions are not availabe in the dev environemnt and only in production. Its clear that the more the load we are receiving this exceptions


Looking at the exceptions in WL 12c (JPA 2.0)looks like there is a duplicate key exception/locking exception, but the same code runs fine in production in WL 10 (JPA .10)


There exceptions are more during peak traffic to the site and subside during teh end of day.


For locking exceptions, i guess the row is locked and the other thread trying to update the row is kicked out.


But why these are not happeing in WL 10 (JPA 1.0 )version. I read that JPA 1.0 uses optimistic locking. Is it that i need to as JPA 2.0 to use optimistic locking.


In the current application we have not made any explicit settings for JPA 1.0 . All default settings og JPA 1.0 are worknig fine in WL10


Is it teh framework updating the table agani causing Duplicatekey exception and do i have to change any setting for JPA 2.0 to remove the locking


Please help no clue as whats going on



Internal Exception: java.sql.SQLIntegrityConstraintViolationException: [FMWGEN][SQLServer JDBC Driver][SQLServer]Violation of PRIMARY KEY constraint 'ICATTRT1'. Cannot insert duplicate key in object 'dbo.TCALTRT'. The duplicate key value is (00n821640331201413.36.14.6, SC, 6).
Error Code: 2627
Call: INSERT INTO TCALTRT (PRC_STZ_DSC_TE, PRC_STZ_NA, REC_UDT_TS, TRS_DUR_MSE_QY, TRS_TM_REC_TYP_CD, TRS_TYP_CD, REC_SEQ_NR, CAL_ID_NR) VALUES (?, ?, ?, ?, ?, ?, ?, ?)


Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLTransactionRollbackException: [FMWGEN][SQLServer JDBC Driver][SQLServer]Transaction (Process ID 213) was deadlocked on lock | communication buffer resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
Error Code: 1205
Call: UPDATE TCALCNX SET CAL_CNX_B = ?, REC_UDT_TS = ? WHERE (CAL_ID_NR = ?)
bind => [3 parameters bound]


Update Exception (Locking)happening in this method



public CallContent CreateNew(String callID) throws DuplicateKeyException{
CallContent pojo = new CallContent();
pojo.setCallID(callID);

/*
CallContentEntityLocalHome entityHome = getEntityHome();
*/

boolean bNew = false;
CallContentEntity entity = entityManager.find(CallContentEntity.class, callID);
if (null != entity)
{
log.debug("Existing CallContent found for call Id");
if (!entity.getRecordStatusCode().equals(
CallContentEntity.RECORDSTATUS_MARKED_FOR_DELETION))
throw new DuplicateKeyException("CallContent Entity already exists. CallID=" + callID);
//re-using an entry that was previously marked for deletion.
entity.setRecordStatusCode(CallContentEntity.RECORDSTATUS_ADDED);
entity.setUpdateTimestamp(new java.sql.Timestamp(Calendar.getInstance().getTimeInMillis()));
}
else
{
log.debug("No existing Call content, create new CallContentEntity");
bNew = true;
entity = new CallContentEntity(callID);
}


entity.setCallContent(serialize(pojo));

if (bNew)
entityManager.persist(entity);
else
entityManager.merge(entity);
return pojo;
}


@Entity
@Table(name="TCALCNX")
public class CallContentEntity implements Serializable {


public static final String RECORDSTATUS_ADDED = "01";
public static final String RECORDSTATUS_UPDATED = "02";
public static final String RECORDSTATUS_MARKED_FOR_DELETION = "15";


@Id
@Column(name="CAL_ID_NR")
private String CallID;

@Column(name="CAL_CNX_B")
private byte[] CallContent;

@Column(name="REC_UDT_TS")
private Timestamp UpdateTimestamp;

@Column(name="REC_STS_CD", length=2)
private String RecordStatusCode;
private static final long serialVersionUID = 1L;



public CallContentEntity() {
super();
}


Duplicate Key exception



public void CreateNew(LoggerInfo linfo, String CallId)
throws DuplicateKeyException {

List<LogPoint> logpoints = linfo.getPoints();

writeAlerts(linfo.getAlerts()); // Write the alerts first
for (int i=0; i < logpoints.size(); i++)
{
try
{
userTransaction.begin();
LogEntity entity = new LogEntity();
LogEntityPK pk = new LogEntityPK();
pk.setCalIdNr(CallId);
pk.setTrsTypCd(logpoints.get(i).getServerName() + IvrUtils.To2DigitWidthString(i));
entity.setPk(pk);
entity.setSerNa(logpoints.get(i).getServerName());
entity.setPrcNa(logpoints.get(i).getProcessName());
java.util.GregorianCalendar tgc = new java.util.GregorianCalendar();
java.util.TimeZone gmttimezone = java.util.TimeZone.getTimeZone("GMT");
tgc.setTimeZone(gmttimezone);
java.util.Date gmttoday = tgc.getTime();
java.util.Date today = new java.util.Date();
java.sql.Timestamp ts = new java.sql.Timestamp(today.getTime());
java.sql.Timestamp gts = new java.sql.Timestamp(gmttoday.getTime());
ts.setTime((logpoints.get(i).getTimeinMilliSeconds()));
entity.setRecUdtTs(ts);
entity.setTrsCrtDt(gts);
entity.setTrsCrtTm(gts);
entity.setTrsLogRsnTe((logpoints.get(i).getLogReason()));
entity.setTrsDatTe((logpoints.get(i).getLogData()));
entityManager.persist(entity);
userTransaction.commit();

}
catch (Exception e)
{
log.error("Log Entity Exception. \n Failed to write log entry:("
+ IvrUtils.formatNumberWithWidth4(i ) + "): "
+ logpoints.get(i).getLogData()
+ "\nException stack: ", e);
}
}

}

@Entity
@Table(name="TCALTRS")
public class LogEntity implements Serializable {
@EmbeddedId
private LogEntityPK pk;

@Column(name="TRS_CRT_DT")
private Timestamp trsCrtDt;

@Column(name="TRS_CRT_TM")
private Timestamp trsCrtTm;

@Column(name="PRC_NA")
private String prcNa;

@Column(name="TRS_DAT_TE")
private String trsDatTe;

@Column(name="TRS_LOG_RSN_TE")
private String trsLogRsnTe;

@Column(name="REC_UDT_TS")
private Timestamp recUdtTs;

@Column(name="SER_NA")
private String serNa;

@Column(name="CUS_INF_RQT_NR")
private String cusInfRqtNr;

private static final long serialVersionUID = 1L;

public LogEntity() {
super();
}


asked 42 secs ago






Aucun commentaire:

Enregistrer un commentaire