Transaction and Connection in DAO
Hi All,
I have Tables
TB_Employee,TB_Salery,TB_Documents
having there relevant attributes.
I have different DAOs EmployeeDAO,SaleryDAO,DocumentsDAO inheriting from BaseDAO having getConnection() and closeConnection()
The above DAOs have corresponding insert,update,delete and Select methods
say EmployeeDAO.insertEmployee() or SaleryDAO.insertSalery(Employee) and
insertDocuments(Employee)
Now my BusinessLayer has method such as generateSalery(Employee).
{
SaleryDAO.insertsalery(Employee)
DocumentsDAO.updateSalerySlipGenerated(Employee)
EmployeeDAO.SaleryProcessed(Employee)
}
So this operation will affect all the three tables.
TB_Salery - insert into TB_Salery set sal=1000 where employeID=Employee.getID();
TB_Documents -update TB_Documents set salgen='y' where employeID=Employee.getID();
TB_Employee - update TB_Employee set docProcesed ='y' where employeID=Employee.getID();
Problem:
1)I dont want to pass the same Connection object from my business layer.Is there any other way?By not exposing Connection to Business Layer.
2)Following the above how to maintain Transaction integrity if i dont pass Connection object.
say updation of TB_Documents failed .I should roll back completly.
I am not using Spring or Hibernate
Create one more DAO :
public class TransactionDAO extends BaseDAO {
.....................
.....................
public static void performOperation ()
{
conn = super.getConection();
/*
To maintain atomicity and consistency . pass the conn object as params to following methods :
*/
SaleryDAO.insertsalery(Employee,conn)
DocumentsDAO.updateSalerySlipGenerated(Employee,conn)
EmployeeDAO.SaleryProcessed(Employee,conn)
conn.commit() ;
...........
conn.close() ;
.............
/* Plz complete other part of the code */
}
You mean one more DAO per transaction. This will only work for this use case.
I don't see any difference between doing it this way and letting the service that knows the transaction boundaries manage the connection. The service and persistence packages are already coupled. What's wrong with having the service manage Connection and XA?
%
Hi guys,I have already stated that I dont want to pass connection object in my method.Creating a another DAO will clutter the code? with method for each Transaction ?Is there a Design Pattern existing for such scenario.I am Using Weblogic Oracle struts and JSP.
IMHO, Oracle should be managing the connections. So why not do a DataSource lookup via JNDI?
> I have already stated that I dont want to pass> connection object in my method.Somehow the connection must be maintained. That is how databases work. And it has nothing to do with java nor jdbc.There are several ways to do it. Pick one.
> Hi All,
> I have Tables
> TB_Employee,TB_Salery,TB_Documents
> having there relevant attributes.
>
> I have different DAOs
> EmployeeDAO,SaleryDAO,DocumentsDAO inheriting from
> BaseDAO having getConnection() and closeConnection()
>
>
> The above DAOs have corresponding
> insert,update,delete and Select methods
> say EmployeeDAO.insertEmployee() or
> SaleryDAO.insertSalery(Employee) and
> insertDocuments(Employee)
>
> Now my BusinessLayer has method such as
> generateSalery(Employee).
> {
> SaleryDAO.insertsalery(Employee)
> DocumentsDAO.updateSalerySlipGenerated(Employee)
> EmployeeDAO.SaleryProcessed(Employee)
> }
> So this operation will affect all the three tables.
>
> TB_Salery - insert into TB_Salery set sal=1000 where
> employeID=Employee.getID();
> TB_Documents -update TB_Documents set salgen='y'
> where employeID=Employee.getID();
> TB_Employee - update TB_Employee set docProcesed ='y'
> where employeID=Employee.getID();
>
> Problem:
> 1)I dont want to pass the same Connection object from
> my business layer.Is there any other way?By not
> exposing Connection to Business Layer.
> 2)Following the above how to maintain Transaction
> integrity if i dont pass Connection object.
> say updation of TB_Documents failed .I should roll
> back completly.
>
>
> I am not using Spring or Hibernate
just encapsulate dao creation, connection establishment and transaction inside a service class (maybe more than one interface, to respect SRP), instantiated and configured by a "custom framework" : a short static factory class configured by the means of files or JNDI
> > Hi All,
> > I have Tables
> > TB_Employee,TB_Salery,TB_Documents
> > having there relevant attributes.
> >
> > I have different DAOs
> > EmployeeDAO,SaleryDAO,DocumentsDAO inheriting from
> > BaseDAO having getConnection() and
> closeConnection()
> >
> >
> > The above DAOs have corresponding
> > insert,update,delete and Select methods
> > say EmployeeDAO.insertEmployee() or
> > SaleryDAO.insertSalery(Employee) and
> > insertDocuments(Employee)
> >
> > Now my BusinessLayer has method such as
> > generateSalery(Employee).
> > {
> > SaleryDAO.insertsalery(Employee)
> > DocumentsDAO.updateSalerySlipGenerated(Employee)
> > EmployeeDAO.SaleryProcessed(Employee)
> > }
> > So this operation will affect all the three
> tables.
> >
> > TB_Salery - insert into TB_Salery set sal=1000
> where
> > employeID=Employee.getID();
> > TB_Documents -update TB_Documents set salgen='y'
> > where employeID=Employee.getID();
> > TB_Employee - update TB_Employee set docProcesed
> ='y'
> > where employeID=Employee.getID();
> >
> > Problem:
> > 1)I dont want to pass the same Connection object
> from
> > my business layer.Is there any other way?By not
> > exposing Connection to Business Layer.
> > 2)Following the above how to maintain Transaction
> > integrity if i dont pass Connection object.
> > say updation of TB_Documents failed .I should roll
> > back completly.
> >
> >
> > I am not using Spring or Hibernate
>
> just encapsulate dao creation, connection
> establishment and transaction inside a service class
> (maybe more than one interface, to respect SRP),
> instantiated and configured by a "custom framework" :
> a short static factory class configured by the means
> of files or JNDI
s/SRP/ISP ;)
> Is there a Design Pattern existing for such scenario.
Checkout the Connection pool pattern. You can make BaseDAO responsible for obtaining the connection from the Pool. However your design approach of using fine grained Dao's can be problematic with connection limited pools. It would typically require more connections. You can move this capability to a compositor of Dao's to mitigate this restriction. The compositor doesnt (shouldn't) need to be part of your Business layer.
> I am Using Weblogic Oracle struts and JSP.
Most Application Servers, include WebLogic and OC4J include built in connection pooling capability.
> Create one more DAO :
> public class TransactionDAO extends BaseDAO {
> .....................
>
> .....................
>
> public static void performOperation ()
> {
> conn = super.getConection();
> /*
> To maintain atomicity and consistency . pass the conn
> object as params to following methods :
> */
> SaleryDAO.insertsalery(Employee,conn)
> DocumentsDAO.updateSalerySlipGenerated(Employee,con
> n)
> EmployeeDAO.SaleryProcessed(Employee,conn)
The reason i posted this code ....
if any one the above operation is felled [insertSalary , UpdateSalarySlip ,SalaryProcessed ], we can do rollback ...
The DB will be in a consistent state ...
And to estabilish physical connection , we can use Service locator [JNDI] ..
or we can create a connection using JDBC apis ....
i.e
connection =DriverManager.getConnection("DriverMgr","userId","PassWd") ....
Code for getting this physical connection can be put in Base Class .
> Hi guys,
> I have already stated that I dont want to pass
> connection object in my method
This may create some prb with Consistency ,.....
Does all the Three opearations are Atomic or not ?
[ Atomicity =Either all operations are performed or None on them ] ..
If not then u can use Service locator pattern ... Otherwise .... U have to pass same connection object to all 3 transaction , so that in case of exeption thrown by one of the operation , u can safely roll back ......
I