Session bean transaction problem?

M

mike

Hi,

In my stateless session bean I have the following code ( stateless
session bean). Then I have a test (test stateless session bean). When
I run the test I can see that I get the first queue record in the db.
Then nothing more happens. The second queue record in my test class is
never added to db.

There are no exceptions in the console and I can only see that the
test is hanging.
Is there something with the transaction that is wrong?

Any help is appreciated.

Sincerely,

//mike


stateless session bean
=================
@Stateless
public class QueueServiceImpl implements QueueService {
private static final Logger logger = Logger
.getLogger(QueueServiceImpl.class.getName());

@PersistenceContext(unitName = "queueservice")
private EntityManager manager;

// Used for testing.
public void changeEntityManager(EntityManager em) {
logger
.finest("Changing entitymanager for unit test outside
EJB container");
this.manager = em;
}

/**
* Fetches all queue records for a specific queueId.
*/
@SuppressWarnings("unchecked")
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public Collection<QueueRecord> fetchQueue(Integer queueId)
throws QueueFinderException {
logger.finest("Get all queue records for queueId=" + queueId);

if (queueId == null) {
throw new IllegalArgumentException(
"Illegal argument for fetchQueue, queueId=" +
queueId);
}

Collection<QueueRecord> queue = manager.createNamedQuery(
"QueueRecord.fetchQueue").setParameter("queueId",
queueId)
.getResultList();

return queue;
}

@SuppressWarnings("unchecked")
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void addToQueue(Integer sessionId, Integer queueId,
Integer maxNrInQueue, long maxTimeInQueue)
throws QueueFinderException {
logger.finest("Add to queue with queueId=" + queueId);

if (queueId == null) {
throw new IllegalArgumentException(
"Illegal argument for fetchQueue, queueId=" +
queueId);
}

// check that we are not reaching max number in queue
int nrAlreadyInQueue = getQueueSize(queueId).intValue();

if ((maxNrInQueue.intValue() != 0)
&& (nrAlreadyInQueue >= maxNrInQueue.intValue())) {
logger.info("addToQueue(): Queue full for queueId=" +
queueId
+ ", sessionId=" + sessionId + ", current queue
length="
+ nrAlreadyInQueue + ", maxNrInQueue=" +
maxNrInQueue);

// TODO: Add exception????
}

long enteredTime = System.currentTimeMillis();

QueueRecord qr = new QueueRecord();
qr.setSessionId(sessionId);
qr.setQueueId(queueId);
qr.setEnteredTime(enteredTime);
manager.persist(qr);

}

@SuppressWarnings("unchecked")
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public Integer getQueueSize(Integer queueId) {

if (queueId == null) {
throw new IllegalArgumentException(
"Illegal argument for getQueueSize, queueId=" +
queueId);
}

int nrRecordsInQueue = 0;

try {
Collection<QueueRecord> queue = fetchQueue(queueId);

if (queue == null) {
logger.severe("getQueueSize(): Error when retrieving
queue");

return null;
}

Iterator<QueueRecord> it = queue.iterator();

while (it.hasNext()) {
nrRecordsInQueue++;
}

logger.fine("getQueueSize(): Found " + nrRecordsInQueue
+ " records");

} catch (QueueFinderException qfe) {
// TODO: Add exception
}
return Integer.valueOf(nrRecordsInQueue);
}

test stateless session bean
====================
public class QueueServiceTest extends BasicTestFeatures {

/** Logger */
private static final Logger logger = Logger.getLogger
(QueueServiceTest.class.getName());

private static final Integer QUEUE_ID = Integer.valueOf(1);

private static final Integer QR1_SESSION_ID = Integer.valueOf(1);
private static final Integer QR1_MAX_NR_IN_QUEUE = Integer.valueOf
(10);
private static final long QR1_TIME_IN_QUEUE = 2L;

private static final Integer QR2_SESSION_ID = Integer.valueOf(2);
private static final Integer QR2_MAX_NR_IN_QUEUE = Integer.valueOf
(10);
private static final long QR2_TIME_IN_QUEUE = 1L;
private QueueServiceImpl qs;


final Mockery context = new JUnit4Mockery();


public QueueServiceTest() { }

@Before
public void setUp() throws Exception {

try {
QueueServiceImpl qsImpl = (QueueServiceImpl) getSessionBean
(QueueService.class, QueueServiceImpl.class);

qs = qsImpl;

} catch (Exception e) {
rollbackTransaction();
fail(e.getMessage());
}

}

@After
public void tearDown() throws Exception { }

public void initQueueRecords() {

try {
beginTransaction();
qs.addToQueue(QR1_SESSION_ID, QUEUE_ID,
QR1_MAX_NR_IN_QUEUE, QR1_TIME_IN_QUEUE);
commitTransaction();
beginTransaction();
qs.addToQueue(QR2_SESSION_ID, QUEUE_ID,
QR2_MAX_NR_IN_QUEUE, QR2_TIME_IN_QUEUE);
commitTransaction();
} catch (Exception e) {
logger.fine("Could not add to queue");
rollbackTransaction();
}

}


@Test
public void testAddToQueue() throws Exception {
initQueueRecords();
//check if we have two records in the queue.

}


}


return Integer.valueOf(nrRecordsInQueue);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top