Friday, 10 August 2018

Oracle | Search the text from the My_Table’s BLOB column

Query to search the text in BLOB column

We need to replace the HEX_CODE_OF_TEXT_TO_SEARCH by the hex code of text.

Online Hex code converter

Hex code of text 'ABC' is '414243'.

Now, the search query for the text 'ABC' in blob column will be like:
WHERE  DBMS_LOB.instr(MT.BLOB_COL_NAME, '414243')>0;

Tuesday, 7 August 2018

Design Patterns | Enforce the singleton property with a private constructor or an enum type

A singleton is simply a class that is instantiated exactly once.

Before release 1.5, there were two ways to implement singletons. Both are based on keeping the constructor private and exporting a public static member to provide access to the sole instance.

In one approach, the member is a final field:
//   Singleton with public final field
public class Singleton {
     public static final Singleton INSTANCE = new Singleton();
     private Singleton() { ... }

The main advantage of the public field approach is that the declarations make it clear that the class is a singleton: the public static field is final, so it will always contain the same object reference. The private constructor can be called only once, to initialize the public static final field Singleton.INSTANCE. Singleton instance will exist once the Singleton class is initialized—no more, no less. However, a privileged client can invoke the private constructor using Reflection by AccessibleObject.setAccessible(true) method.

In the second approach to implementing singletons, the public member is a static factory method:
//Singleton with static factory
public class Singleton {
     private static final Singleton INSTANCE = new Singleton();
     private Singleton() { ... }
     public static Singleton getInstance() { return INSTANCE; }

One advantage of the factory-method approach is that it gives you the flexibility to change your mind about whether the class should be a singleton without changing its API. The factory method returns the sole instance but could easily be modified to return, say, a unique instance for each thread that invokes it. A second advantage, concerning generic types.

To make a singleton class that is implemented using either of the previous approaches serializable, it is not sufficient merely to add implements Serializable to its declaration.
Each time a serialized instance is deserialized, a new instance will be created. To maintain the singleton guarantee, you have to declare all instance fields transient and provide a readResolve method.

//readResolve method to preserve singleton property
private Object readResolve() {
     // Return the one true Singleton and let the garbage collector
     // take care of the Singleton impersonator.
     return INSTANCE;

In Java, a privileged client can invoke the private constructor using Reflection by AccessibleObject.setAccessible(true) method and we can instantiate the Singleton class again. To avoid it, we can use an enum based approach.

Singleton using enum
As of release 1.5, there is a third approach to implementing singletons. This approach is functionally equivalent to the public field approach, except that it is more concise, provides the serialization machinery for free, and provides the guarantee against multiple instantiations, even in the face of sophisticated serialization or reflection attacks.

public enum Singleton {
     public void doStuff(){
           System.out.println("Singleton using Enum");

Enum is thread-safe which helps us to avoid double checking (=less code for better results).
While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton.

And this can be called from clients,
public static void main(String[] args) {

Related Posts Plugin for WordPress, Blogger...