Thursday, 12 January 2017

Write a program to create deadlock between two threads.

Deadlock describes a situation where two or more threads are blocked forever, waiting for each other to release a lock.

Code:
package com.javakickoff
public class MyDeadlockClass {

 String str1 = "Oracle";
 String str2 = "MySQL";
 
 Thread trd1 = new Thread("My Thread 1"){
  public void run(){
  while(true){
     synchronized(str1){
        synchronized(str2){
           System.out.println(str1 + str2);
     }
    }
   }
  }
 };
 
 Thread trd2 = new Thread("My Thread 2"){
  public void run(){
  while(true){
     synchronized(str2){
        synchronized(str1){
           System.out.println(str2 + str1);
     }
    }
   }
  }
 };
 
 public static void main(String a[]){
  MyDeadlockClass mdl = new MyDeadlockClass();
  mdl.trd1.start();
  mdl.trd2.start();
 }
}

How to call stored procedure in Hibernate

MySQL store procedure

Here’s a MySQL store procedure, which accept a stock code parameter and return the related stock data.


DELIMITER $$
CREATE PROCEDURE `GetStocks`(int_stockcode varchar(20))
BEGIN
   SELECT * FROM stock where stock_code = int_stockcode;
   END $$

DELIMITER ; 
In MySQL, you can simple call it with a call keyword : 
CALL GetStocks('7277');

Hibernate call store procedure

In Hibernate, there are three approaches to call a database store procedure.

1. Native SQL – createSQLQuery

You can use createSQLQuery() to call a store procedure directly.
 Query query = session.createSQLQuery(
 "CALL GetStocks(:stockCode)")
 .addEntity(Stock.class)
 .setParameter("stockCode", "7277");

List result = query.list();
for(int i=0; i<result.size(); i++){
 Stock stock = (Stock)result.get(i);
 System.out.println(stock.getStockCode());
} 

2. NamedNativeQuery in annotation

Declare your store procedure inside the @NamedNativeQueries annotation.
 //Stock.java 
...  
@NamedNativeQueries({ 
 @NamedNativeQuery( 
 name = "callStockStoreProcedure", 
 query = "CALL GetStocks(:stockCode)", 
 resultClass = Stock.class ) }) 

@Entity  
@Table(name = "stock") 
public class Stock implements java.io.Serializable {

Call it with getNamedQuery().
Query query = session.getNamedQuery("callStockStoreProcedure")
 .setParameter("stockCode", "7277");
List result = query.list();
for(int i=0; i<result.size(); i++){
 Stock stock = (Stock)result.get(i);
 System.out.println(stock.getStockCode());
} 

3. sql-query in XML mapping file 

Declare your store procedure inside the "sql-query" tag. 

<!-- Stock.hbm.xml --> 

...

<hibernate-mapping>
    <class name="com.mkyong.common.Stock" table="stock" ...>
        <id name="stockId" type="java.lang.Integer">
            <column name="STOCK_ID" />
            <generator class="identity" />
        </id>
        <property name="stockCode" type="string">
            <column name="STOCK_CODE" length="10" 
        not-null="true" unique="true" />
        </property>
        ...
    </class>

    <sql-query name="callStockStoreProcedure">
 <return alias="stock" class="com.mkyong.common.Stock"/>
 <![CDATA[CALL GetStocks(:stockCode)]]>
    </sql-query>

</hibernate-mapping> 
Call it with getNamedQuery().
Query query = session.getNamedQuery("callStockStoreProcedure")
 .setParameter("stockCode", "7277");
List result = query.list();
for(int i=0; i<result.size(); i++){
 Stock stock = (Stock)result.get(i);
 System.out.println(stock.getStockC

Why multiple inheritance is not supported in java?

First of all try to understand what is diamond problem?

Consider the below diagram which shows multiple inheritance as Class D extends both Class B & C. Now lets say we have a method in class A and Class B & C overrides that method in their own way. Now problem comes here – 

Assume, method hello() is overridden in Class B and C.

Class D is extending both B & C, so now if Class D wants to use the same method i.e. hello(), which method would be called ? the overridden method hello() of B or or C ?.  Because of this ambiguity java doesn’t support multiple inheritance.

Also, multiple inheritances does complicate the design and creates problem during casting, constructor chaining etc and given that there are not many scenario on which you need multiple inheritance its wise decision to omit it for the sake of simplicity. Also java avoids this ambiguity by supporting single inheritance with interfaces. Since interface only have method declaration and doesn't provide any implementation there will only be just one implementation of specific method hence there would not be any ambiguity.

However, you can achieve multiple inheritance in Java using interfaces. Assume in above diagram B and C is interface not a class, then class D can implement the method hello() and this will be common implementation in class D.

interface B
{
   public void hello();
}
interface C
{
   public void hello();
}
class D implements B, C
{
   public void hello()
   {
       System.out.println(" Multiple inheritance example using interfaces");
   }
}