JDBC

Business Transactions

  • The process of retrieving a value from database, changing it and pushing it back to database is termed as a business transaction.
  • Business transaction encompass one or more database transactions.
  • Following things must be taken care while handling business transactions.
    • Inconsistent data must be handled before writing to database.
    • If inconsistent data cannot be handled then the database should be rolled back to previous persistent state. 

Transaction Management
  • Transactions is a very important topic in various Database related persistent technologies like hibernate and  JPA there transaction management is a separate topic.
  • After learning hibernate we will learn Spring in Spring we have a separate topic called as transactions.
  • Transactions is a set of related operations which should be executed all together or none should be executed.
  • The various properties of a transaction are as follows
    • Atomicity
    • Consistency
    • Isolation
    • Durability
  • There are 2 types of transactions
    • Local Transactions
      • All operations are performed on same database
      • JDBC only supports local transactions
    • Global Transactions
      • Operations are performed on multiple databases.
      • If we want to go for global transactions we need support from EJB or Spring Framework.
  • To implement transactions in JDBC we use 3 methods of Connection Interface.
    • con.setAutoCommit(false);
      • We need to set autoCommit to false initially.
    • con.Commit();
      • If all the operations complete successfully we use con.commit() to finalize changes to database.
    • con.rollback();
      • is used to rollback if any operations fails.
    • We also need to catch SQLException in case of any problems while performing operations and call rollback in that block.
Connection Pooling
  • It is a method to keep database connection open so that they can be reused.
  • Opening a database connection is an expensive process so we keep the existing one opened and ready to use when a database connection is requested.
  • Server first checks the pool for a suitable connection if one is available it is given to the client else a new connection is created.
  • Rather than getting connection object through DriverManager class,we get them through an instance of the DataSource class.
  • DataSource object maintains a pool of persistent database connections in order to eliminate the time consuming step of establishing a database connection each time the application interacts with the database.
Configuring the Data Source
  • There are 2 ways to configure a datasource
    • First way is to include the library for a DataSource implementation and create DataSource by interacting with the library.
    • Second way is to tell the web application container(like Tomcat etc) to create DataSource for us.
      • This approach is referred to as container managed connection pooling because the web application container that hosts the web application creates and destroys the data source that implements connection pooling.
  • A DataSource is a static property in a servlet
    • This helps in DataSource pooling as only one instance is passed to the multiple instances of the servlet.
    • Secondly DataSource is already created even before Servlet is created so we do not have to check the creation of Servlet Instance First and then the DataSource.
  • When using DataSource Connection pooling, it is important to close connections and their associated objects when finished with them.
    • If this is not done the pool of available connections inside the DataSource will become exhausted and application threads will block and wait indefinitely for an available connection.
Object Persistence
  • Code devoted to manage the persistent state of objects compromises a large percentage of total code.
  • Because this type of code can be generalized libraries have been provided which act as a framework to implement object persistence in their applications.
    • These frameworks are referred to as object-relational mapping(ORM) frameworks because they provide the means to move data represented as objects in code into and out of relational databases.
    • Two well known open Source ORM frameworks include Hibernate and iBatis

No comments:

Post a Comment