Based on the following code before the try/catch/finally block, fill out what sh
ID: 3928018 • Letter: B
Question
Based on the following code before the try/catch/finally block, fill out what should go into the try/catch/finally blocks in order to handle a transaction that executes the below DELETE Statement. Also, specify the code that should go into the finally block to close the EntityManager Object entityManager.
Assume that there are accounts whose statuses are old.
EntityTransaction transaction = entityManager.getTransaction();
String qStr = "DELETE FROM Account a WHERE a.Status = :Status";
Query qObj = entityManager.createQuery(qStr);
qObj.setParameter(Status, 'Old');
int numRowsAffected = 0;
try
{
}
catch(Exception e)
{
}
finally
{
}
Explanation / Answer
try
{
EntityTransaction transaction = entityManager.getTransaction();
String qStr = "DELETE FROM Account a WHERE a.Status = :Status";
Query qObj = entityManager.createQuery(qStr);
qObj.setParameter(Status, 'Old');
int numRowsAffected = 0;
}
catch(Exception e)
{
System.out.println(e.printStackTrace());
}
finally
{
if(qObj!=null)
qObj.close();
if(transaction!=null)
transaction.close();
}try
{
EntityTransaction transaction = entityManager.getTransaction();
String qStr = "DELETE FROM Account a WHERE a.Status = :Status";
Query qObj = entityManager.createQuery(qStr);
qObj.setParameter(Status, 'Old');
int numRowsAffected = 0;
}
catch(Exception e)
{
System.out.println(e.printStackTrace());
}
finally
{
if(qObj!=null)
qObj.close();
if(transaction!=null)
transaction.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.