PDA

Click to See Complete Forum and Search --> : Help in application structure


Death_Knight
August 19th, 2004, 12:53 PM
I have a application that updates several table at 1 time. But now i want to make sure that any failure in updating any 1 of the tables, the database would rollback to the original state. How should i design the structure of my application?

JSP page --- >> Servlet --- >> Controller --- >> Database

In my jsp page, this is where the user enters the data and submit. The servlet would capture all the data that the user enter into the textfield. The Controller class is a file, which i code to manage the updating of the records and also responsible for calling other classes to update to the database. For the database class it is just a class i coded to connect to database, and accept SQL statements through a method to perform insert,update,delete,query.

In my controller, i would call Class A and B to perform update operations.

so in Class A and B i would have something that look like this


public class A
....
public void updateRecord()
{
String sql = // here goes my sql update statement...
Database db = new Database();
db.executeUpdate(sql);
}


in my database class

public class Database
....
public void executeUpdate(String sql)
{
// all the update to database goes here
}


in my controller class

public class UpdateController()
{
A a = new A();
a.updateRecord();

B b = new B();
b.updateRecord();
}


where should i insert the codes to ensure the updating of the records smoothly?

dspeidel
August 19th, 2004, 10:23 PM
Rusty here long time since I've played with tables at the programatical level but don't you have to do a comitt for the changes to stay? So if I you do x updates wait on doing the comitt until all x updates are successful, if any of the x fail do a roll back.

Cheers,
-D

nihil
August 19th, 2004, 10:34 PM
Death_Knight,

I am not sure of your question here..........are you wanting to update an entire record, or allow partial updates?

I am guessing that you are holding your database in more than one table?

I need a little bit more information, but I would guess that you should create forms for each section of the database, then link those?

cheers

chsh
August 19th, 2004, 11:16 PM
This should have info on what you are looking for: http://www.fawcette.com/javapro/archives/bs0999/

What you are discussing is called transaction processing, it uses Commit and Rollback functionality to ensure the inserts/updates/deletes/whatever are proper before applying any one set of changes.

Death_Knight
August 20th, 2004, 04:08 AM
http://www.fawcette.com/javapro/archives/bs0999/ posted by chsh.

Well, my application works somehow similar like this.
If you're using an ATM to transfer funds between your savings and checking accounts, at some point, the amount being transferred is subtracted from one account (step 1) and added to the other (step 2).

I will briefly describe the classes in my application what it does:

Database.java, a generic database class used by all classes performing database operations.
NewController.java, a specific class responsible for creating a customer record.
Customer.java, this class contain all the sql statements for /insert/update/delete/select operations.
Account.java, this class contain all the sql statements for /insert/update/delete/select operations.

the flow of my application

my Servlet would request all the information from the jsp page. For example


String name = request.getParameter("customerName");
String accountId = request.getParameter("accountId");

Customer customer = new Customer(name);
Account account = new Account(accountId);

NewController controller = new NewController(customer, account);


In my NewController class, i will call the methods in the Customer and Account class

public NewController(Customer c, Account a)
{
c.createCustomer();
a.createAccount();
}


So in my Customer and Account class

public void createCustomer()
{
Database db = new Database();
db.executeUpdate("insert into blah blah blah"):
}


In this case, whenever i create a new customer record, i need to create a account for the customer as well. But in any of the scenario, it may be the creation of the customer record fail, or the creation of the account record fail, how should i alter the structure of my application ?

Sorry for the bad english and my bad explanation