Sunday, February 17, 2013

Dependency Injection ( DI )

Dependency Injection means inversion of responsibilities with regard to how an object obtains references to collaborating objects.

When applying DI , objects are given their dependencies at creation time by some external entity that coordinates with each object in the system.

Benefits : Loose coupling.

Friday, February 15, 2013

Define DDL , DML , TCL

Data Definition Language ( DDL ) :
Data Definition Language are used to define the structure of a database or schema.
Some of the example includes statements like CREATE , ALTER , DROP etc.
Oracle Database implicitly commits the current transaction before and after every DDL statement.

Examples :
  • CREATE - can be used to create a database , table , stored procedure.
  • ALTER - can be used to alter the structure of databse table like add a new column.
  • DROP - can be used to drop objects from database like drop a table.
  • TRUNCATE - remove all records from a table . also remove all spaces allocated for the recods.
  • COMMENT - can be used to add comments to data dictionary.
  • RENAME - renames an object

            Data Manipulation Language ( DML ) :
            Data Manipulation Language can be used to modify / alter the data of existing schema objects. 
            DML statements do not implicitly commit the current transactions.

            Example :
            • SELECT - used to retrive data from a database table.
            • INSERT - used to insert new record in a table.
            • UPDATE - can be used to update column values of a table.
            • DELETE - can be used to delete records from a table.
            • MERGE - insert or update operation.
            • EXPLAIN PLAN - explain access path to data.
            • CALL - can be used to call PL/SQL.
            • LOCK TABLE - controls the concurrency.

            Transaction Control Statements ( TCL ) :
            Transaction Control Statements are used to control the manipulations made by Data Manipulation Language ( DML ) statements . 

            Example :
            • COMMIT - used to commit the transaction so that any changes made by the query are reflected by the database table.
            • ROLLBACK - revert the changes that were made in the last commit.
            • SAVEPOINT -  creates / identifies a point in the transaction to which you can rollback later.
            • SET TRANSACTION - change transaction options like isolation level.

            Session Control Statements  :
            Session control statements dynamically manage the properties of a user session. These statements do not implicitly commit the current transaction.

            Example :
            • ALTER SESSION
            • SET ROLE

            Wednesday, February 13, 2013

            Immutable object in java

            Immutable objects are the  one whose state can't be changed once they are constructed. Example objects of class String and Integer. Immutable objects are considered useful in concurrent applications because threads can't change their state.

            How to create an immutable object ?

            1. Ensure that the class can't be overridden . The simplest way is to make the class final.   Another way is to make the construstor private and use factory methods to construct instances.

            2. Make all the fields private and final.

            3. Do not provide any methods that can change object's state . For example  don't provide any setter methods to change object's state.

            4. If the instance members refer to any mutable objects, don't allow those objects to get changed. This can be done in following ways :

            • Don't provide methods that can modify the mutable objects.
            • Don't share references to the mutable objects.
            • Never store references to external, mutable objects passed to the constructor. if necessary, create copies, and store references to the copies.
            • Never return the original mutable object in the methods, if necessary create copies of mutable objects and return them.

            To demonstrate this behavior , we can take example of class java.lang.String

            String oldString = new String("Old String");

            System.out.println("Before : " + oldString);
            oldString.replace("Old", "new");
            System.out.println("After  : " + oldString);

            The output will be:

            Before : Old String
            After     : Old String

            Here we can see that the contents of the string oldString did not change. It is because the objects of class String are immutable.