import javax.transaction.*;
public class TransactionExample {
public void doTransaction() {
try {
UserTransaction transaction = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
transaction.begin();
// Perform database or resource operations here
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
// Handle exception and roll back transaction if needed
}
}
}