use of org.compiere.util.Trx in project adempiere by adempiere.
the class PreparedStatementProxy method init.
// PreparedStatementProxy
/**
* Initialise the prepared statement wrapper object
*/
protected void init() {
try {
Connection conn = null;
Trx trx = p_vo.getTrxName() == null ? null : Trx.get(p_vo.getTrxName(), false);
if (trx != null) {
conn = trx.getConnection();
} else {
if (p_vo.getResultSetConcurrency() == ResultSet.CONCUR_UPDATABLE)
m_conn = DB.getConnectionRW();
else
m_conn = DB.getConnectionRO();
conn = m_conn;
}
if (conn == null)
throw new DBException("No Connection");
p_stmt = conn.prepareStatement(p_vo.getSql(), p_vo.getResultSetType(), p_vo.getResultSetConcurrency());
} catch (Exception e) {
log.log(Level.SEVERE, p_vo.getSql(), e);
throw new DBException(e);
}
}
use of org.compiere.util.Trx in project adempiere by adempiere.
the class StatementProxy method init.
/**
* Initialise the statement wrapper object
*/
protected void init() {
try {
Connection conn = null;
Trx trx = p_vo.getTrxName() == null ? null : Trx.get(p_vo.getTrxName(), false);
if (trx != null) {
conn = trx.getConnection();
} else {
if (p_vo.getResultSetConcurrency() == ResultSet.CONCUR_UPDATABLE)
m_conn = DB.getConnectionRW();
else
m_conn = DB.getConnectionRO();
conn = m_conn;
}
if (conn == null)
throw new DBException("No Connection");
p_stmt = conn.createStatement(p_vo.getResultSetType(), p_vo.getResultSetConcurrency());
} catch (SQLException e) {
log.log(Level.SEVERE, "CStatement", e);
throw new DBException(e);
}
}
use of org.compiere.util.Trx in project adempiere by adempiere.
the class CallableStatementProxy method init.
/**
* Initialise the prepared statement wrapper object
*/
protected void init() {
try {
Connection conn = null;
Trx trx = p_vo.getTrxName() == null ? null : Trx.get(p_vo.getTrxName(), true);
if (trx != null) {
conn = trx.getConnection();
} else {
if (p_vo.getResultSetConcurrency() == ResultSet.CONCUR_UPDATABLE)
m_conn = DB.getConnectionRW();
else
m_conn = DB.getConnectionRO();
conn = m_conn;
}
if (conn == null)
throw new DBException("No Connection");
p_stmt = conn.prepareCall(p_vo.getSql(), p_vo.getResultSetType(), p_vo.getResultSetConcurrency());
return;
} catch (Exception e) {
log.log(Level.SEVERE, p_vo.getSql(), e);
throw new DBException(e);
}
}
use of org.compiere.util.Trx in project adempiere by adempiere.
the class ClientAcctProcessor method postSession.
// doIt
/**
* Post Session
*/
private void postSession() {
List<BigDecimal> listProcessedOn = new ArrayList<BigDecimal>();
for (Timestamp dateacct : getListDateAcct()) {
listProcessedOn.clear();
// to include potential null values
listProcessedOn.add(Env.ZERO);
//get current time from db
Timestamp ts = DB.getSQLValueTS(get_TrxName(), "SELECT CURRENT_TIMESTAMP FROM DUAL");
//go back 2 second to be safe (to avoid posting documents being completed at this precise moment)
long ms = ts.getTime() - (2 * 1000);
ts = new Timestamp(ms);
long mili = ts.getTime();
BigDecimal value = new BigDecimal(Long.toString(mili));
//first pass, collect all ts (FR 2962094 - required for weighted average costing)
int[] documentsTableID = Doc.getDocumentsTableID();
String[] documentsTableName = Doc.getDocumentsTableName();
for (int i = 0; i < documentsTableID.length; i++) {
int AD_Table_ID = documentsTableID[i];
String TableName = documentsTableName[i];
// Post only special documents
if (p_AD_Table_ID != 0 && p_AD_Table_ID != AD_Table_ID)
continue;
StringBuffer sql = new StringBuffer("SELECT DISTINCT ProcessedOn FROM ").append(TableName).append(" WHERE AD_Client_ID=? AND ProcessedOn<?").append(" AND Processed='Y' AND Posted='N' AND IsActive='Y' and " + getColumnNameDateAcct(AD_Table_ID) + " = ? ");
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
pstmt.setInt(1, getAD_Client_ID());
pstmt.setBigDecimal(2, value);
pstmt.setTimestamp(3, dateacct);
rs = pstmt.executeQuery();
while (rs.next()) {
BigDecimal processedOn = rs.getBigDecimal(1);
if (!listProcessedOn.contains(processedOn))
listProcessedOn.add(processedOn);
}
} catch (Exception e) {
log.log(Level.SEVERE, sql.toString(), e);
} finally {
DB.close(rs, pstmt);
}
}
// initialize counters per table
int[] count = new int[documentsTableID.length];
int[] countError = new int[documentsTableID.length];
for (int i = 0; i < count.length; i++) {
count[i] = 0;
countError[i] = 0;
}
//sort and post in the processed date order
Collections.sort(listProcessedOn);
for (BigDecimal processedOn : listProcessedOn) {
for (int i = 0; i < documentsTableID.length; i++) {
int AD_Table_ID = documentsTableID[i];
String TableName = documentsTableName[i];
// Post only special documents
if (p_AD_Table_ID != 0 && p_AD_Table_ID != AD_Table_ID)
continue;
// SELECT * FROM table
StringBuffer sql = new StringBuffer("SELECT * FROM ").append(TableName).append(" WHERE AD_Client_ID=? AND (ProcessedOn");
if (processedOn.compareTo(Env.ZERO) != 0)
sql.append("=?");
else
sql.append(" IS NULL OR ProcessedOn=0");
sql.append(") AND Processed='Y' AND Posted='N' AND IsActive='Y'").append(" ORDER BY Created");
//
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
pstmt.setInt(1, getAD_Client_ID());
if (processedOn.compareTo(Env.ZERO) != 0)
pstmt.setBigDecimal(2, processedOn);
rs = pstmt.executeQuery();
while (rs.next()) {
count[i]++;
boolean ok = true;
// Run every posting document in own transaction
String innerTrxName = Trx.createTrxName("CAP");
Trx innerTrx = Trx.get(innerTrxName, true);
String postStatus = Doc.STATUS_NotPosted;
Doc doc = Doc.get(m_ass, AD_Table_ID, rs, innerTrxName);
try {
if (doc == null) {
log.severe(getName() + ": No Doc for " + TableName);
ok = false;
} else {
// post no force/repost
String error = doc.post(false, false);
ok = (error == null);
postStatus = doc.getPostStatus();
}
} catch (Exception e) {
log.log(Level.SEVERE, getName() + ": " + TableName, e);
ok = false;
} finally {
if (ok)
innerTrx.commit();
else {
innerTrx.rollback();
// save the posted status error (out of trx)
StringBuffer sqlupd = new StringBuffer("UPDATE ").append(doc.get_TableName()).append(" SET Posted='").append(postStatus).append("',Processing='N' ").append("WHERE ").append(doc.get_TableName()).append("_ID=").append(doc.get_ID());
DB.executeUpdateEx(sqlupd.toString(), null);
}
innerTrx.close();
innerTrx = null;
}
if (!ok)
countError[i]++;
}
} catch (Exception e) {
log.log(Level.SEVERE, sql.toString(), e);
} finally {
DB.close(rs, pstmt);
}
}
// for tableID
}
for (int i = 0; i < documentsTableID.length; i++) {
String TableName = documentsTableName[i];
if (count[i] > 0) {
m_summary.append(TableName).append("=").append(count[i]);
if (countError[i] > 0)
m_summary.append("(Errors=").append(countError[i]).append(")");
m_summary.append(" - ");
log.finer(getName() + ": " + m_summary.toString());
} else
log.finer(getName() + ": " + TableName + " - no work");
}
}
}
use of org.compiere.util.Trx in project adempiere by adempiere.
the class PO method delete.
// decrypt
/**************************************************************************
* Delete Current Record
* @param force delete also processed records
* @return true if deleted
*/
public boolean delete(boolean force) {
CLogger.resetLast();
if (is_new())
return true;
int AD_Table_ID = p_info.getAD_Table_ID();
int Record_ID = get_ID();
if (!force) {
int iProcessed = get_ColumnIndex("Processed");
if (iProcessed != -1) {
Boolean processed = (Boolean) get_Value(iProcessed);
if (processed != null && processed.booleanValue()) {
// CannotDeleteTrx
log.warning("Record processed");
log.saveError("Processed", "Processed", false);
return false;
}
}
// processed
}
// force
Trx localTrx = null;
boolean success = false;
try {
String localTrxName = m_trxName;
if (localTrxName == null) {
localTrxName = Trx.createTrxName("POdel");
localTrx = Trx.get(localTrxName, true);
m_trxName = localTrxName;
}
try {
if (!beforeDelete()) {
log.warning("beforeDelete failed");
return false;
}
} catch (Exception e) {
log.log(Level.WARNING, "beforeDelete", e);
log.saveError("Error", e, false);
// throw new DBException(e);
return false;
}
// Delete Restrict AD_Table_ID/Record_ID (Requests, ..)
String errorMsg = PO_Record.exists(AD_Table_ID, Record_ID, m_trxName);
if (errorMsg != null) {
log.saveError("CannotDelete", errorMsg);
return false;
}
// Call ModelValidators TYPE_DELETE
errorMsg = ModelValidationEngine.get().fireModelChange(this, isReplication() ? ModelValidator.TYPE_BEFORE_DELETE_REPLICATION : ModelValidator.TYPE_DELETE);
// @Trifon
setReplication(false);
if (errorMsg != null) {
log.saveError("Error", errorMsg);
return false;
}
//
deleteTranslations(localTrxName);
// Delete Cascade AD_Table_ID/Record_ID (Attachments, ..)
PO_Record.deleteCascade(AD_Table_ID, Record_ID, localTrxName);
// The Delete Statement
StringBuffer sql = //jz why no FROM??
new StringBuffer("DELETE FROM ").append(p_info.getTableName()).append(" WHERE ").append(get_WhereClause(true));
int no = 0;
if (isUseTimeoutForUpdate())
no = DB.executeUpdateEx(sql.toString(), localTrxName, QUERY_TIME_OUT);
else
no = DB.executeUpdate(sql.toString(), localTrxName);
success = no == 1;
// Save ID
m_idOld = get_ID();
//
if (!success) {
log.warning("Not deleted");
if (localTrx != null)
localTrx.rollback();
} else {
if (success) {
MSession session = MSession.get(p_ctx, false);
if (session == null)
log.fine("No Session found");
else if (Ini.isPropertyBool(Ini.P_LOGMIGRATIONSCRIPT))
session.logMigration(this, p_info, MMigrationStep.ACTION_Delete);
if (p_info.isChangeLog()) {
// Change Log
if (session != null && m_IDs.length == 1) {
int AD_ChangeLog_ID = 0;
int size = get_ColumnCount();
for (int i = 0; i < size; i++) {
Object value = m_oldValues[i];
if (value != null && // logging allowed
p_info.isAllowLogging(i) && // not encrypted
!p_info.isEncrypted(i) && // no virtual column
!p_info.isVirtualColumn(i) && !"Password".equals(p_info.getColumnName(i))) {
// change log on delete
MChangeLog cLog = session.changeLog(m_trxName != null ? m_trxName : localTrxName, AD_ChangeLog_ID, AD_Table_ID, p_info.getColumn(i).AD_Column_ID, Record_ID, getAD_Client_ID(), getAD_Org_ID(), value, null, MChangeLog.EVENTCHANGELOG_Delete);
if (cLog != null)
AD_ChangeLog_ID = cLog.getAD_ChangeLog_ID();
}
}
// for all fields
}
// Housekeeping
m_IDs[0] = I_ZERO;
if (m_trxName == null)
log.fine("complete");
else
log.fine("[" + m_trxName + "] - complete");
m_attachment = null;
}
} else {
log.warning("Not deleted");
}
}
try {
success = afterDelete(success);
} catch (Exception e) {
log.log(Level.WARNING, "afterDelete", e);
log.saveError("Error", e, false);
success = false;
// throw new DBException(e);
}
// Call ModelValidators TYPE_AFTER_DELETE - teo_sarca [ 1675490 ]
if (success) {
errorMsg = ModelValidationEngine.get().fireModelChange(this, ModelValidator.TYPE_AFTER_DELETE);
if (errorMsg != null) {
log.saveError("Error", errorMsg);
success = false;
}
}
if (!success) {
if (localTrx != null)
localTrx.rollback();
} else {
if (localTrx != null) {
try {
localTrx.commit(true);
} catch (SQLException e) {
log.saveError("Error", e);
success = false;
}
}
}
// Reset
if (success) {
m_idOld = 0;
int size = p_info.getColumnCount();
m_oldValues = new Object[size];
m_newValues = new Object[size];
CacheMgt.get().reset(p_info.getTableName());
}
} finally {
if (localTrx != null) {
localTrx.close();
m_trxName = null;
}
}
// log.info("" + success);
return success;
}
Aggregations