Skip to main content

Handling Debit and Credit Operations

In this document, we will explain the process of handling debit and credit operations on a bank account. The process involves retrieving account details, opening a database connection, and updating the account balance.

The flow starts by logging the entry and retrieving the account details. If the account is found, it opens a connection to the database and prepares SQL statements for querying and updating the account balance. The method then updates the account's actual and available balances and executes the update SQL statement. If the account is not found, it logs a warning and exits.

Flow drill down


Handling account debit and credit operations

First, the debitCredit method is responsible for handling the debit and credit operations on an account. It starts by logging the entry and then retrieves the account details using the getAccount method. If the account is not found, it logs a warning and exits. If the account is found, it opens a connection to the database and prepares SQL statements for querying and updating the account balance. The method then updates the account's actual and available balances and executes the update SQL statement.

	public boolean debitCredit(BigDecimal apiAmount)
{
logger.entering(this.getClass().getName(), DEBIT_CREDIT_ACCOUNT);
Account temp = this.getAccount(
Integer.parseInt(this.getAccountNumber()),
Integer.parseInt(this.getSortcode()));
if (temp == null)
{
logger.log(Level.WARNING,
() -> "Unable to find account " + this.getAccountNumber());
logger.exiting(this.getClass().getName(), DEBIT_CREDIT_ACCOUNT,
false);
return false;
}

openConnection();
String accountNumberString = temp.getAccountNumber();

String sortCodeString = padSortCode(
Integer.parseInt(this.getSortcode()));
String sql1 = SQL_SELECT;


Retrieving account details

Next, the getAccount method retrieves the account details from the database. It opens a connection and prepares SQL statements to query the account based on the account number and sort code. If the account number is a special value (99999999), it uses a different SQL query. The method then processes the result set to create an Account object with the retrieved details.

	public Account getAccount(int accountNumber, int sortCode)
{
logger.entering(this.getClass().getName(), GET_ACCOUNT + accountNumber);
openConnection();
Account temp = null;

String sortCodeString = padSortCode(sortCode);
String sql9999 = "SELECT * from ACCOUNT where ACCOUNT_EYECATCHER LIKE 'ACCT' AND ACCOUNT_SORTCODE like ? order by ACCOUNT_NUMBER DESC";
String sql = SQL_SELECT;
try (PreparedStatement stmt9999 = conn.prepareStatement(sql9999);
PreparedStatement stmt = conn.prepareStatement(sql);)
{
if (accountNumber == 99999999)
{

logger.log(Level.FINE, () -> PRE_SELECT_MSG + sql9999 + ">");

stmt9999.setString(1, sortCodeString);
ResultSet rs = stmt9999.executeQuery();
if (rs.next())
{


Opening a database connection

Then, the openConnection method is responsible for opening a connection to the DB2 database. It checks if a connection already exists for the current task and reuses it if available. If the connection is closed or does not exist, it calls the openConnectionInternal method to create a new connection.

	protected void openConnection()
{
// Open a connection to the DB2 database
logger.entering(this.getClass().getName(), "openConnection()");

Integer taskNumberInteger = Task.getTask().getTaskNumber();
String db2ConnString = DB2CONN.concat(taskNumberInteger.toString());
logger.log(Level.FINE,
() -> "Attempting to get DB2CONN for task number "
+ taskNumberInteger.toString());
this.conn = (Connection) cornedBeef.get(db2ConnString);
if (this.conn == null)
{
HBankDataAccess.incrementConnCount();
logger.log(Level.FINE,
() -> "Attempting to create DB2CONN for task number "
+ taskNumberInteger.toString());
// Attempt to open a connection
openConnectionInternal();
logger.log(Level.FINE,
() -> "Creation succcessful for DB2CONN for task number "


Creating a new database connection

Finally, the openConnectionInternal method creates a new database connection using a JNDI lookup. It retrieves the data source and attempts to get a connection. If successful, it sets the transaction isolation level and stores the connection in a map for reuse. If any errors occur, it logs the error and triggers an abend (abnormal end) for the task.

	@SuppressWarnings("unchecked")
void openConnectionInternal()
{
logger.entering(this.getClass().getName(), "openConnectionInternal");
String jndiString = "jdbc/defaultCICSDataSource";
Context ctx;

try
{
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup(jndiString);
logger.log(Level.FINE, () -> "jndi string is " + jndiString);
// If there is no current connection
if (this.conn == null)
{
logger.log(Level.FINE,
() -> "About to attempt to get DB2 connection");
// Try and get a connection
this.conn = ds.getConnection();
this.conn.setTransactionIsolation(
Connection.TRANSACTION_READ_UNCOMMITTED);

Where is this flow used?

This flow is used once, in a flow starting from transferLocalExternal as represented in the following diagram:

 

This is an auto-generated document by Swimm 🌊 and has not yet been verified by a human