use of org.apache.ofbiz.entity.transaction.GenericTransactionException in project ofbiz-framework by apache.
the class IdealEvents method idealNotify.
/**
* iDEAL notification
*/
public static String idealNotify(HttpServletRequest request, HttpServletResponse response) {
Locale locale = UtilHttp.getLocale(request);
Delegator delegator = (Delegator) request.getAttribute("delegator");
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
Map<String, Object> parametersMap = UtilHttp.getParameterMap(request);
String transactionId = request.getParameter("trxid");
for (String name : parametersMap.keySet()) {
String value = request.getParameter(name);
Debug.logError("### Param: " + name + " => " + value, module);
}
String orderId = null;
String paymentStatus = null;
try {
IdealConnector connector = new IdealConnector("payment");
Transaction transaction = connector.requestTransactionStatus(transactionId);
orderId = transaction.getPurchaseID();
if (orderId == null) {
orderId = (String) request.getSession().getAttribute("purchaseID");
}
String payAmount = transaction.getAmount();
if (payAmount == null) {
payAmount = (String) request.getSession().getAttribute("payAmount");
}
paymentStatus = transaction.getStatus();
request.setAttribute("transactionId", transactionId);
request.setAttribute("paymentStatus", paymentStatus);
request.setAttribute("paymentAmount", payAmount);
} catch (IdealException ex) {
Debug.logError(ex.getMessage(), module);
request.setAttribute("_ERROR_MESSAGE_", ex.getConsumerMessage());
return "error";
}
// get the user
if (userLogin == null) {
String userLoginId = "system";
try {
userLogin = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", userLoginId).queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, "Cannot get UserLogin for: " + userLoginId + "; cannot continue", module);
request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resourceErr, "idealEvents.problemsGettingAuthenticationUser", locale));
return "error";
}
}
// get the order header
GenericValue orderHeader = null;
if (UtilValidate.isNotEmpty(orderId)) {
try {
orderHeader = EntityQuery.use(delegator).from("OrderHeader").where("orderId", orderId).queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, "Cannot get the order header for order: " + orderId, module);
request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resourceErr, "idealEvents.problemsGettingOrderHeader", locale));
return "error";
}
} else {
Debug.logError("iDEAL did not callback with a valid orderId!", module);
request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resourceErr, "idealEvents.noValidOrderIdReturned", locale));
return "error";
}
if (orderHeader == null) {
Debug.logError("Cannot get the order header for order: " + orderId, module);
request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resourceErr, "idealEvents.problemsGettingOrderHeader", locale));
return "error";
}
// attempt to start a transaction
boolean okay = true;
boolean beganTransaction = false;
try {
beganTransaction = TransactionUtil.begin();
// authorized
if ("Success".equals(paymentStatus)) {
okay = OrderChangeHelper.approveOrder(dispatcher, userLogin, orderId);
// cancelled
} else if ("Cancelled".equals(paymentStatus)) {
okay = OrderChangeHelper.cancelOrder(dispatcher, userLogin, orderId);
}
if (okay) {
// set the payment preference
okay = setPaymentPreferences(delegator, dispatcher, userLogin, orderId, request);
}
} catch (Exception e) {
String errMsg = "Error handling iDEAL notification";
Debug.logError(e, errMsg, module);
try {
TransactionUtil.rollback(beganTransaction, errMsg, e);
} catch (GenericTransactionException gte2) {
Debug.logError(gte2, "Unable to rollback transaction", module);
}
} finally {
if (!okay) {
try {
TransactionUtil.rollback(beganTransaction, "Failure in processing iDEAL callback", null);
} catch (GenericTransactionException gte) {
Debug.logError(gte, "Unable to rollback transaction", module);
}
} else {
try {
TransactionUtil.commit(beganTransaction);
} catch (GenericTransactionException gte) {
Debug.logError(gte, "Unable to commit transaction", module);
}
}
}
if (okay) {
request.setAttribute("_EVENT_MESSAGE_", UtilProperties.getMessage(resource, "IdealSuccessful", locale));
// call the email confirm service
Map<String, String> emailContext = UtilMisc.toMap("orderId", orderId, "userLogin", userLogin);
try {
dispatcher.runSync("sendOrderConfirmation", emailContext);
} catch (GenericServiceException e) {
Debug.logError(e, "Problems sending email confirmation", module);
}
}
return "success";
}
use of org.apache.ofbiz.entity.transaction.GenericTransactionException in project ofbiz-framework by apache.
the class EntitySaxReader method parse.
private long parse(InputStream is, String docDescription) throws SAXException, java.io.IOException {
SAXParser parser;
try {
parser = SAXParserFactory.newInstance().newSAXParser();
} catch (ParserConfigurationException pce) {
throw new SAXException("Unable to create the SAX parser", pce);
}
numberRead = 0;
try {
boolean beganTransaction = false;
if (transactionTimeout > -1) {
beganTransaction = TransactionUtil.begin(transactionTimeout);
Debug.logImportant("Transaction Timeout set to " + transactionTimeout / 3600 + " hours (" + transactionTimeout + " seconds)", module);
}
try {
parser.parse(is, this);
// make sure all of the values to write got written...
if (!valuesToWrite.isEmpty()) {
writeValues(valuesToWrite);
valuesToWrite.clear();
}
if (!valuesToDelete.isEmpty()) {
delegator.removeAll(valuesToDelete);
valuesToDelete.clear();
}
TransactionUtil.commit(beganTransaction);
} catch (GenericEntityException | IOException | IllegalArgumentException | SAXException e) {
String errMsg = "An error occurred saving the data, rolling back transaction (" + beganTransaction + ")";
Debug.logError(e, errMsg, module);
TransactionUtil.rollback(beganTransaction, errMsg, e);
throw new SAXException("A transaction error occurred reading data", e);
}
} catch (GenericTransactionException e) {
throw new SAXException("A transaction error occurred reading data", e);
}
Debug.logImportant("Finished " + numberRead + " values from " + docDescription, module);
if (Debug.verboseOn()) {
Debug.logVerbose(" Detail created : " + numberCreated + ", skipped : " + numberSkipped + ", updated : " + numberUpdated + ", replaced : " + numberReplaced + ", deleted : " + numberDeleted, module);
}
return numberRead;
}
use of org.apache.ofbiz.entity.transaction.GenericTransactionException in project ofbiz-framework by apache.
the class EntityTestSuite method testOneBigTransactionIsFasterThanSeveralSmallOnes.
/*
This test is useful to confirm that the default setting of use-transaction="true" for screen definitions is
the best one for performance.
With this setting one database transaction is started by the framework before rendering the screen.
Most screens usually have multiple Delegator calls to find records.
In most of the GenericDelegator's find methods, a transaction is created if one does not already exist,
the statement is performed, and the transaction is committed.
So, by making that attribute "false" - a screen will have many individual transactions instead of just one.
This test assess that running the same number of sql statements withing one transaction is faster than running
them with individual transactions.
*/
public void testOneBigTransactionIsFasterThanSeveralSmallOnes() {
boolean transactionStarted = false;
long startTime = System.currentTimeMillis();
int totalNumberOfRows = 0;
int numberOfQueries = 500;
boolean noErrors = true;
try {
transactionStarted = TransactionUtil.begin();
for (int i = 1; i <= numberOfQueries; i++) {
List rows = EntityQuery.use(delegator).from("SequenceValueItem").queryList();
totalNumberOfRows = totalNumberOfRows + rows.size();
}
TransactionUtil.commit(transactionStarted);
} catch (GenericEntityException e) {
try {
TransactionUtil.rollback(transactionStarted, "", e);
} catch (GenericTransactionException e2) {
}
noErrors = false;
}
long endTime = System.currentTimeMillis();
long totalTimeOneTransaction = endTime - startTime;
Debug.logInfo("Selected " + totalNumberOfRows + " rows with " + numberOfQueries + " queries (all contained in one big transaction) in " + totalTimeOneTransaction + " ms", module);
assertTrue("Errors detected executing the big transaction", noErrors);
totalNumberOfRows = 0;
noErrors = true;
try {
for (int i = 1; i <= numberOfQueries; i++) {
transactionStarted = TransactionUtil.begin();
List rows = EntityQuery.use(delegator).from("SequenceValueItem").queryList();
totalNumberOfRows = totalNumberOfRows + rows.size();
TransactionUtil.commit(transactionStarted);
}
} catch (GenericEntityException e) {
try {
TransactionUtil.rollback(transactionStarted, "", e);
} catch (Exception e2) {
}
noErrors = false;
}
endTime = System.currentTimeMillis();
long totalTimeSeveralSmallTransactions = endTime - startTime;
Debug.logInfo("Selected " + totalNumberOfRows + " rows with " + numberOfQueries + " queries (each in its own transaction) in " + totalTimeSeveralSmallTransactions + " ms", module);
assertTrue("Errors detected executing the small transactions", noErrors);
assertTrue("One big transaction was not faster than several small ones", totalTimeOneTransaction < totalTimeSeveralSmallTransactions);
}
use of org.apache.ofbiz.entity.transaction.GenericTransactionException in project ofbiz-framework by apache.
the class SQLProcessor method getConnection.
/**
* Get a connection from the TransactionFactoryLoader
*
* @return The connection created
*
* @throws GenericDataSourceException
* @throws GenericEntityException
*/
public Connection getConnection() throws GenericDataSourceException, GenericEntityException {
if (_connection != null)
return _connection;
_manualTX = true;
try {
_connection = TransactionFactoryLoader.getInstance().getConnection(helperInfo);
if (Debug.verboseOn())
Debug.logVerbose("SQLProcessor:connection() : manualTx=" + _manualTX, module);
} catch (SQLException sqle) {
throw new GenericDataSourceException("Unable to establish a connection with the database.", sqle);
}
// make sure we actually did get a connection
if (_connection == null) {
throw new GenericDataSourceException("Unable to establish a connection with the database. Connection was null!");
}
// test the connection
testConnection(_connection);
// always try to set auto commit to false, but if we can't then later on we won't commit
try {
if (_connection.getAutoCommit()) {
try {
_connection.setAutoCommit(false);
if (Debug.verboseOn())
Debug.logVerbose("SQLProcessor:setAutoCommit(false) : manualTx=" + _manualTX, module);
} catch (SQLException sqle) {
_manualTX = false;
}
}
} catch (SQLException e) {
throw new GenericDataSourceException("Cannot get autoCommit status from connection", e);
}
try {
if (TransactionUtil.getStatus() == TransactionUtil.STATUS_ACTIVE) {
if (Debug.verboseOn())
Debug.logVerbose("[SQLProcessor.getConnection] : active transaction", module);
_manualTX = false;
}
} catch (GenericTransactionException e) {
// nevermind, don't worry about it, but print the exc anyway
Debug.logWarning("[SQLProcessor.getConnection]: Exception was thrown trying to check " + "transaction status: " + e.toString(), module);
}
if (Debug.verboseOn())
Debug.logVerbose("[SQLProcessor.getConnection] : con=" + _connection, module);
_bDeleteConnection = true;
return _connection;
}
use of org.apache.ofbiz.entity.transaction.GenericTransactionException in project ofbiz-framework by apache.
the class LoginWorker method setLoggedOut.
public static void setLoggedOut(String userLoginId, Delegator delegator) {
if (UtilValidate.isEmpty(userLoginId)) {
if (Debug.warningOn()) {
Debug.logWarning("Called setLogged out with empty userLoginId", module);
}
}
Transaction parentTx = null;
boolean beganTransaction = false;
try {
try {
parentTx = TransactionUtil.suspend();
} catch (GenericTransactionException e) {
Debug.logError(e, "Cannot suspend current transaction: " + e.getMessage(), module);
}
try {
beganTransaction = TransactionUtil.begin();
GenericValue userLogin = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", userLoginId).queryOne();
if (userLogin == null) {
Debug.logError("Could not find UserLogin record for setLoggedOut with userLoginId [" + userLoginId + "]", module);
} else {
userLogin.set("hasLoggedOut", "Y");
userLogin.store();
}
} catch (GenericEntityException e) {
String errMsg = "Unable to set logged out flag on UserLogin";
Debug.logError(e, errMsg, module);
try {
TransactionUtil.rollback(beganTransaction, errMsg, e);
} catch (GenericTransactionException e2) {
Debug.logError(e2, "Could not rollback nested transaction: " + e.getMessage(), module);
}
} finally {
try {
TransactionUtil.commit(beganTransaction);
} catch (GenericTransactionException e) {
Debug.logError(e, "Could not commit nested transaction: " + e.getMessage(), module);
}
}
} finally {
// resume/restore parent transaction
if (parentTx != null) {
try {
TransactionUtil.resume(parentTx);
if (Debug.verboseOn()) {
Debug.logVerbose("Resumed the parent transaction.", module);
}
} catch (GenericTransactionException ite) {
Debug.logError(ite, "Cannot resume transaction: " + ite.getMessage(), module);
}
}
}
}
Aggregations