Skip to content

Commit c2b962c

Browse files
stuartwdouglaskabir
authored andcommittedAug 7, 2012
AS7-5291 server calls getPrimaryKey() internally which can result in exception depending on current invocation context state
1 parent 1ab6d19 commit c2b962c

24 files changed

+121
-90
lines changed
 

‎cmp/src/main/java/org/jboss/as/cmp/TransactionEntityMap.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@
2626
import java.util.Collections;
2727
import java.util.List;
2828
import java.util.Map;
29+
2930
import javax.ejb.EJBException;
3031
import javax.transaction.RollbackException;
3132
import javax.transaction.Synchronization;
3233
import javax.transaction.SystemException;
3334
import javax.transaction.Transaction;
3435
import javax.transaction.TransactionManager;
35-
import static org.jboss.as.cmp.CmpMessages.MESSAGES;
36+
3637
import org.jboss.as.cmp.component.CmpEntityBeanComponent;
3738
import org.jboss.as.cmp.context.CmpEntityBeanContext;
3839
import org.jboss.logging.Logger;
@@ -45,6 +46,8 @@
4546
import org.jboss.tm.TransactionLocal;
4647
import org.jboss.tm.TxUtils;
4748

49+
import static org.jboss.as.cmp.CmpMessages.MESSAGES;
50+
4851
/**
4952
* @author John Bailey
5053
*/
@@ -140,7 +143,7 @@ public void scheduleSync(Transaction tx, CmpEntityBeanContext context) {
140143
}
141144

142145
public void invokeEjbStore(Thread thread, CmpEntityBeanContext context) throws Exception {
143-
if (!context.isRemoved() && context.getPrimaryKey() != null) {
146+
if (!context.isRemoved() && context.getPrimaryKeyUnchecked() != null) {
144147
CmpEntityBeanComponent container = context.getComponent();
145148
// set the context class loader before calling the store method
146149
container.invokeEjbStore(context);
@@ -150,7 +153,7 @@ public void invokeEjbStore(Thread thread, CmpEntityBeanContext context) throws E
150153
public void synchronize(Thread thread, Transaction tx, CmpEntityBeanContext context) throws Exception {
151154
// only synchronize if the id is not null. A null id means
152155
// that the entity has been removed.
153-
if (!context.isRemoved() && context.getPrimaryKey() != null) {
156+
if (!context.isRemoved() && context.getPrimaryKeyUnchecked() != null) {
154157
CmpEntityBeanComponent container = context.getComponent();
155158

156159
// set the context class loader before calling the store method
@@ -180,7 +183,7 @@ public void scheduleSync(Transaction tx, CmpEntityBeanContext context) {
180183
public void synchronize(Thread thread, Transaction tx, CmpEntityBeanContext context) throws Exception {
181184
CmpEntityBeanComponent container = context.getComponent();
182185
if (container.getStoreManager().isStoreRequired(context)) {
183-
throw MESSAGES.instanceEvictedBeforeSync(container.getComponentName(), context.getPrimaryKey());
186+
throw MESSAGES.instanceEvictedBeforeSync(container.getComponentName(), context.getPrimaryKeyUnchecked());
184187
}
185188
}
186189

@@ -314,7 +317,7 @@ public void synchronize() {
314317
if (causeByException instanceof EJBException) {
315318
throw (EJBException) causeByException;
316319
}
317-
throw CmpMessages.MESSAGES.failedToStoreEntity(((context == null || context.getPrimaryKey() == null) ? "<null>" : context.getPrimaryKey().toString()), causeByException);
320+
throw CmpMessages.MESSAGES.failedToStoreEntity(((context == null || context.getPrimaryKeyUnchecked() == null) ? "<null>" : context.getPrimaryKeyUnchecked().toString()), causeByException);
318321
} finally {
319322
synchronizing = false;
320323
}

‎cmp/src/main/java/org/jboss/as/cmp/component/CmpEntityBeanComponent.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public Object invoke(final CMRMessage message, final Object key, final Object...
121121
* @throws Exception
122122
*/
123123
public void invokeEjbStore(CmpEntityBeanContext ctx) throws Exception {
124-
if (ctx.getPrimaryKey() != null) {
124+
if (ctx.getPrimaryKeyUnchecked() != null) {
125125
// if call-ejb-store-for-clean=true then invoke ejbStore first (the last chance to modify the instance)
126126
if (ejbStoreForClean) {
127127
try {
@@ -150,7 +150,7 @@ public void invokeEjbStore(CmpEntityBeanContext ctx) throws Exception {
150150
}
151151

152152
public void storeEntity(CmpEntityBeanContext ctx) throws Exception {
153-
if (ctx.getPrimaryKey() != null) {
153+
if (ctx.getPrimaryKeyUnchecked() != null) {
154154
if (getStoreManager().isStoreRequired(ctx)) {
155155
getStoreManager().storeEntity(ctx);
156156
}

‎cmp/src/main/java/org/jboss/as/cmp/component/CmpEntityBeanComponentInstance.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public synchronized void store() {
119119

120120
final CmpEntityBeanContext context = getEjbContext();
121121
final JDBCEntityPersistenceStore store = getComponent().getStoreManager();
122-
if (context.getPrimaryKey() != null && store.isStoreRequired(context)) {
122+
if (context.getPrimaryKeyUnchecked() != null && store.isStoreRequired(context)) {
123123
store.storeEntity(context);
124124
}
125125
}

‎cmp/src/main/java/org/jboss/as/cmp/component/interceptors/CmpEntityBeanJdbcRelationshipInterceptor.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public Object processInvocation(InterceptorContext context) throws Exception {
5757
if (CMRMessage.GET_RELATED_ID == relationshipMessage) {
5858
// call getRelateId
5959
if (log.isTraceEnabled()) {
60-
log.trace("Getting related id: field=" + cmrField.getFieldName() + " id=" + ctx.getPrimaryKey());
60+
log.trace("Getting related id: field=" + cmrField.getFieldName() + " id=" + ctx.getPrimaryKeyUnchecked());
6161
}
6262
return cmrField.getRelatedId(ctx);
6363

@@ -66,7 +66,7 @@ public Object processInvocation(InterceptorContext context) throws Exception {
6666
Object relatedId = context.getParameters()[1];
6767
if (log.isTraceEnabled()) {
6868
log.trace("Add relation: field=" + cmrField.getFieldName() +
69-
" id=" + ctx.getPrimaryKey() +
69+
" id=" + ctx.getPrimaryKeyUnchecked() +
7070
" relatedId=" + relatedId);
7171
}
7272

@@ -79,7 +79,7 @@ public Object processInvocation(InterceptorContext context) throws Exception {
7979
Object relatedId = context.getParameters()[1];
8080
if (log.isTraceEnabled()) {
8181
log.trace("Remove relation: field=" + cmrField.getFieldName() +
82-
" id=" + ctx.getPrimaryKey() +
82+
" id=" + ctx.getPrimaryKeyUnchecked() +
8383
" relatedId=" + relatedId);
8484
}
8585

‎cmp/src/main/java/org/jboss/as/cmp/context/CmpEntityBeanContext.java

+4
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,8 @@ public TransactionEntityMap.TxAssociation getTxAssociation() {
9292
public void setTxAssociation(TransactionEntityMap.TxAssociation txAssociation) {
9393
this.txAssociation = txAssociation;
9494
}
95+
96+
public Object getPrimaryKeyUnchecked() {
97+
return getComponentInstance().getPrimaryKey();
98+
}
9599
}

‎cmp/src/main/java/org/jboss/as/cmp/jdbc/CascadeDeleteStrategy.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public void cascadeDelete(CmpEntityBeanContext ctx, List oldValues) throws Remov
159159
}
160160

161161
if (didDelete) {
162-
executeDeleteSQL(batchCascadeDeleteSql, ctx.getPrimaryKey());
162+
executeDeleteSQL(batchCascadeDeleteSql, ctx.getPrimaryKeyUnchecked());
163163
}
164164
}
165165
}

‎cmp/src/main/java/org/jboss/as/cmp/jdbc/JDBCAbstractCreateCommand.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
import java.util.ArrayList;
2929
import java.util.Date;
3030
import java.util.List;
31+
3132
import javax.ejb.CreateException;
33+
3234
import org.jboss.as.cmp.CmpMessages;
33-
import static org.jboss.as.cmp.CmpMessages.MESSAGES;
3435
import org.jboss.as.cmp.context.CmpEntityBeanContext;
3536
import org.jboss.as.cmp.jdbc.bridge.JDBCCMPFieldBridge;
3637
import org.jboss.as.cmp.jdbc.bridge.JDBCCMRFieldBridge;
@@ -40,6 +41,8 @@
4041
import org.jboss.logging.Logger;
4142
import org.jboss.security.AuthenticationManager;
4243

44+
import static org.jboss.as.cmp.CmpMessages.MESSAGES;
45+
4346
/**
4447
* Base class for create commands that drives the operation sequence.
4548
*
@@ -267,11 +270,11 @@ protected void performInsert(CmpEntityBeanContext ctx) throws CreateException {
267270
// execute statement
268271
int rowsAffected = executeInsert(index, ps, ctx);
269272
if (rowsAffected != 1) {
270-
throw CmpMessages.MESSAGES.expectedOneRow(rowsAffected, ctx.getPrimaryKey());
273+
throw CmpMessages.MESSAGES.expectedOneRow(rowsAffected, ctx.getPrimaryKeyUnchecked());
271274
}
272275
} catch (SQLException e) {
273276
if (exceptionProcessor != null && exceptionProcessor.isDuplicateKey(e)) {
274-
throw CmpMessages.MESSAGES.uniqueKeyViolationInvalidFk(ctx.getPrimaryKey());
277+
throw CmpMessages.MESSAGES.uniqueKeyViolationInvalidFk(ctx.getPrimaryKeyUnchecked());
275278
} else {
276279
throw CmpMessages.MESSAGES.couldNotCreateEntity(e);
277280
}

‎cmp/src/main/java/org/jboss/as/cmp/jdbc/JDBCCustomFinderQuery.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public Collection execute(Method unused, Object[] args, CmpEntityBeanContext ctx
9292
try {
9393
// invoke implementation method on ejb instance
9494
Object value;
95-
final EntityBeanComponentInstance componentInstance = ctx.getComponent().getCache().get(ctx.getPrimaryKey());
95+
final EntityBeanComponentInstance componentInstance = ctx.getComponent().getCache().get(ctx.getPrimaryKeyUnchecked());
9696
try {
9797
value = finderMethod.invoke(componentInstance, args);
9898
} finally {

‎cmp/src/main/java/org/jboss/as/cmp/jdbc/JDBCLoadEntityCommand.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@
2525
import java.sql.PreparedStatement;
2626
import java.sql.ResultSet;
2727
import java.util.List;
28+
2829
import javax.ejb.EJBException;
29-
import static org.jboss.as.cmp.CmpMessages.MESSAGES;
30+
31+
import org.jboss.as.cmp.context.CmpEntityBeanContext;
3032
import org.jboss.as.cmp.jdbc.bridge.JDBCCMPFieldBridge;
3133
import org.jboss.as.cmp.jdbc.bridge.JDBCEntityBridge;
3234
import org.jboss.as.cmp.jdbc.bridge.JDBCFieldBridge;
3335
import org.jboss.as.cmp.jdbc.metadata.JDBCFunctionMappingMetaData;
34-
import org.jboss.as.cmp.context.CmpEntityBeanContext;
3536
import org.jboss.logging.Logger;
3637

38+
import static org.jboss.as.cmp.CmpMessages.MESSAGES;
39+
3740
/**
3841
* JDBCLoadEntityCommand loads the data for an instance from the table.
3942
* This command implements specified eager loading. For CMP 2.x, the
@@ -111,7 +114,7 @@ private boolean execute(JDBCCMPFieldBridge requiredField,
111114
CmpEntityBeanContext ctx,
112115
boolean failIfNotFound) {
113116
// load the instance primary key fields into the context
114-
Object id = ctx.getPrimaryKey();
117+
Object id = ctx.getPrimaryKeyUnchecked();
115118
entity.injectPrimaryKeyIntoInstance(ctx, id);
116119

117120
// get the read ahead cache
@@ -213,7 +216,7 @@ private boolean execute(JDBCCMPFieldBridge requiredField,
213216
// did we load the main results
214217
if (!mainEntityLoaded) {
215218
if (failIfNotFound)
216-
throw MESSAGES.entityNotFound(ctx.getPrimaryKey());
219+
throw MESSAGES.entityNotFound(ctx.getPrimaryKeyUnchecked());
217220
else
218221
return false;
219222
} else

‎cmp/src/main/java/org/jboss/as/cmp/jdbc/JDBCPostCreateEntityCommand.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public Object execute(Method m, Object[] args, CmpEntityBeanContext ctx) {
7676
if (cmrField.isForeignKeyValid(relatedId)) {
7777
cmrField.createRelationLinks(ctx, relatedId);
7878
} else {
79-
relatedCMRField.addRelatedPKWaitingForMyPK(relatedId, ctx.getPrimaryKey());
79+
relatedCMRField.addRelatedPKWaitingForMyPK(relatedId, ctx.getPrimaryKeyUnchecked());
8080
}
8181
} catch (Exception e) {
8282
// no such object

‎cmp/src/main/java/org/jboss/as/cmp/jdbc/JDBCRemoveEntityCommand.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@
2525
import java.sql.Connection;
2626
import java.sql.PreparedStatement;
2727
import java.util.Map;
28+
2829
import javax.ejb.RemoveException;
2930

30-
import static org.jboss.as.cmp.CmpMessages.MESSAGES;
31+
import org.jboss.as.cmp.context.CmpEntityBeanContext;
3132
import org.jboss.as.cmp.jdbc.bridge.JDBCCMRFieldBridge;
3233
import org.jboss.as.cmp.jdbc.bridge.JDBCEntityBridge;
33-
import org.jboss.as.cmp.context.CmpEntityBeanContext;
3434
import org.jboss.logging.Logger;
3535

36+
import static org.jboss.as.cmp.CmpMessages.MESSAGES;
37+
3638

3739
/**
3840
* JDBCRemoveEntityCommand executes a DELETE FROM table WHERE command.
@@ -86,7 +88,7 @@ public JDBCRemoveEntityCommand(JDBCStoreManager manager) {
8688

8789
public void execute(CmpEntityBeanContext ctx) throws RemoveException, RemoteException {
8890
if (entity.isRemoved(ctx)) {
89-
throw MESSAGES.instanceAlreadyRemoved(ctx.getPrimaryKey());
91+
throw MESSAGES.instanceAlreadyRemoved(ctx.getPrimaryKeyUnchecked());
9092
}
9193

9294
entity.setIsBeingRemoved(ctx);
@@ -105,7 +107,7 @@ public void execute(CmpEntityBeanContext ctx) throws RemoveException, RemoteExce
105107
executeDeleteSQL(ctx);
106108
} else {
107109
if (log.isTraceEnabled())
108-
log.trace("Instance is scheduled for cascade delete. id=" + ctx.getPrimaryKey());
110+
log.trace("Instance is scheduled for cascade delete. id=" + ctx.getPrimaryKeyUnchecked());
109111
}
110112
}
111113
// cascade-delete to old relations, if relation uses cascade.
@@ -119,16 +121,16 @@ public void execute(CmpEntityBeanContext ctx) throws RemoveException, RemoteExce
119121
executeDeleteSQL(ctx);
120122
} else {
121123
if (log.isTraceEnabled())
122-
log.debug("Instance is scheduled for cascade delete. id=" + ctx.getPrimaryKey());
124+
log.debug("Instance is scheduled for cascade delete. id=" + ctx.getPrimaryKeyUnchecked());
123125
}
124126
}
125127

126-
manager.getReadAheadCache().removeCachedData(ctx.getPrimaryKey());
128+
manager.getReadAheadCache().removeCachedData(ctx.getPrimaryKeyUnchecked());
127129
entity.setRemoved(ctx);
128130
}
129131

130132
private void executeDeleteSQL(CmpEntityBeanContext ctx) throws RemoveException {
131-
Object key = ctx.getPrimaryKey();
133+
Object key = ctx.getPrimaryKeyUnchecked();
132134
Connection con = null;
133135
PreparedStatement ps = null;
134136
int rowsAffected = 0;

‎cmp/src/main/java/org/jboss/as/cmp/jdbc/JDBCStoreEntityCommand.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void execute(CmpEntityBeanContext ctx) {
6868
if (!dirtyIterator.hasNext() || entity.isBeingRemoved(ctx) || entity.isScheduledForBatchCascadeDelete(ctx)) {
6969
if (log.isTraceEnabled()) {
7070
log.trace("Store command NOT executed. Entity is not dirty "
71-
+ ", is being removed or scheduled for *batch* cascade delete: pk=" + ctx.getPrimaryKey());
71+
+ ", is being removed or scheduled for *batch* cascade delete: pk=" + ctx.getPrimaryKeyUnchecked());
7272
}
7373
return;
7474
}
@@ -119,7 +119,7 @@ public void execute(CmpEntityBeanContext ctx) {
119119
}
120120

121121
// WHERE: set primary key fields
122-
index = entity.setPrimaryKeyParameters(ps, index, ctx.getPrimaryKey());
122+
index = entity.setPrimaryKeyParameters(ps, index, ctx.getPrimaryKeyUnchecked());
123123

124124
// WHERE: set optimistically locked field values
125125
if (hasLockedFields) {
@@ -144,7 +144,7 @@ public void execute(CmpEntityBeanContext ctx) {
144144

145145
// check results
146146
if (rowsAffected != 1) {
147-
throw CmpMessages.MESSAGES.updateFailedTooManyRowsAffected(rowsAffected, ctx.getPrimaryKey());
147+
throw CmpMessages.MESSAGES.updateFailedTooManyRowsAffected(rowsAffected, ctx.getPrimaryKeyUnchecked());
148148
}
149149

150150
// Mark the updated fields as clean.

‎cmp/src/main/java/org/jboss/as/cmp/jdbc/JDBCStoreManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ public boolean loadEntity(CmpEntityBeanContext ctx, boolean failIfNotFound) {
442442
// is any on the data already in the entity valid
443443
if (!ctx.isValid()) {
444444
if (log.isTraceEnabled()) {
445-
log.trace("RESET PERSISTENCE CONTEXT: id=" + ctx.getPrimaryKey());
445+
log.trace("RESET PERSISTENCE CONTEXT: id=" + ctx.getPrimaryKeyUnchecked());
446446
}
447447
entityBridge.resetPersistenceContext(ctx);
448448
}

0 commit comments

Comments
 (0)
Please sign in to comment.