Data Persistency

Data Persistency

Representing Data permanently in Back end systems is called as “Data Persistency”. To achieve Data Persistency in database applications we will use a set of Operations [CRUD] called as Date Persistence Operations”.

To achieve Data Persistency in Enterprise Applications we will use a set of tech called as “Data Persistency Tech”.

To achieve Data Persistency in enterprise applications we will use the following technologies w.r.t JAVA.

  • Serialization and Deserialization
  • JDBC
  • ORM Implementations
    • Hibernate
    • EJBs Entity Beans
    • JPA
    • Open JPA
    • Toplink
Data Persistency through Serialization and Deserialization

The process of Separating Data from an Object is called as “Serialization”. The process of regenerating an object on the basis of Data is called as “Deserialization”.

To perform Serialization and Deserialization JAVA has provided the following two byte oriented streams

  1. Object OutputStream for Serialization
  2. Object InputStream for Deserialization
Steps to perform Serialization
  1. Prepare Serializable class by implementing java.io.Serializable marker interface
class Employee implements Serializable {
}
Employee emp1 = new Employee();

Create FileOutputStream object

FileOutputStream fos = new FileOutputStream("employee.txt");

Create ObjectOutputStream for Serialization.

ObjectOutputStream oos = new ObjectOutputStream(fos);

Write Serializable Object in ObjectOutputStream

oos.writeObject(emp1);
Steps to perform Deserialization

Create FileInputStream from Source File

FileInputStream fis = new FileInputStream("employee.txt");

Create ObjectInputStream for Deserialization

ObjectInputStream ois = new ObjectinputStream(fis);

Read Deserialized Object from ObjectInputStream

Employee emp2 = (Employee)ois.readObject();
  • In Serialization and Deserialization Data Persistency mechanism, we will store data in file system, where file system is providing permanent storage for our data, so that, Serialization and Deserialization is a Data Persistency Mechanism.
  • In Serialization and Deserialization data persistency mechanism, we will use a flat file to store data, it is platform dependent, it is not suitable for the platform independent front end technologies like JAVA, .NET etc
  • In Serialization and Deserialization Data Persistency mechanism, we will use two byte oriented streams like ObjectOutputStream and ObjectInputStream to perform Serialization and Deserialization, so that, this data persistency mechanism is suitable for only byte oriented data.
  • In this data persistency mechanism, we are using file system to store data , it able to store less data , it able to provide less security and it able to increase data redundancy, it is not suggestible in applications.
  • This persistency mechanism is suitable for Standalone applications, not suitable for enterprise applications.
  • If we use this data persistency mechanism in Enterprise applications then we have to provide lot of java code.
  • In Serialization and Deserialization data persistency mechanism we are able to perform
  • only insertion and retrieval operations, we are unable to perform update, delete etc operations.
  • In this data persistency mechanism, query language support is not existed so that database operations are very much difficult.
Data Persistency through JDBC

JDBC is a step by step process or a technology or an API or the collection of predefined classes and interfaces or an abstraction, it will provide very good environment to interact with database from java applications in order to perform database operations from java applications.

To prepare JDBC applications we have to use the following steps.

Load and Register Driver

Class.forName("oracle.jdbc.OracleDriver");

Establish Connection between Java application and Database.

Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "ashok");

Create either Statement or PreparedStatement or CallableStatement as per the requirement.

Statement st = con.createStatement();

Write and execute SQL Queries.

ResultSet rs = st.executeQuery("select * from employee");

Close the resources.

rs.close();
st.close();
con.close()
  • In Jdbc Applications, when we establish Connection between java application and database , automatically, the generated Connection will be available in “Auto-Commit” mode, in this mode, if we submit an SQL query to the Connection then Connection will carry that SQL query to Database Engine and Connection will make Database Engine to execute SQL Query and to results in Database Permanently, this Nature of Connection is able to achieve Data Persistency in JDBC Applications.
  • In JDBC applications, we have provide SQL Queries explicitly to perform database operations, So that, Developers must required SQL Awareness.
  • In JDBC applications, developers must have explicitly concentration on Driver Management, Connection Management and Statement Management.
  • In JDBC Applications, the steps like Load and Register Driver, Establish Connection, Create Statement and Close resources etc. are common ion every JDBC Application, we must write repeatedly, it will increase Code duplication.
  • In Jdbc applications, we have to hard code the JDBC Parameters like Driver class name, Driver URL, Database User Name and Database password etc
  • In JDBC, Transactions support is not good.
  • In JDBC almost all the exceptions are checked exceptions, we must handle them by providing JAVA code.
  • In JDBC applications, if we retrieve data from database then that will be stored in ResultSet object at java applications, it is not implementing java.io.Serializable interface and which is not eligible to carry in network.
Data Persistency
Scroll to top