Thursday, June 30, 2011

Function Inlining and Final methods

In C++ and C# methods donot use polymorphism until unless asked to do so using virtual and override constructs as below.



using System;

public class DrawingObject
{
public virtual void Draw()
{
Console.WriteLine("I'm just a generic drawing object.");
}
}

using System;

public class Line : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}




However in java this is not the case, it is not optional.
However using the final keyword in your code you can tell the compiler that what methods are not allowed to be overridden.

Java compilers depending on their implementations use various techniques for performance optimization, function inlining being one of them i.e.

if a method is not overridden and it is short then the complier can optimize the method call be means od inlining.

e.getName() can be replaced with e.name

This is a major improvement if you think in terms of CPU. CPU's have prefecthing stratery builtin to speed up the FETCH-DECODE-EXECUTE cycle and it hates branching. So code inlining is definately is a speedup.

Fortunately JIT compilers in VM knows exactly which class overrides a given class and they can check whether any class overrides a given method, and hence decide for inlining.

In cases where you are sure that a methods needs not to be overridden, you can avoid this lookup activity/overload by declaring a method as final.

To sum up it makes a lot of sense if you declare all you methods as final unless you have a good reason that you want polymorphism.

Wednesday, June 8, 2011

Maven + MyEclipse the first view

For the first i tried my hands time on maven2. It is such a wonderful tool and using it with MyEclipse is such a charm.

It is so easy to setup a maven project on MyEclipse. All you need to do is to create a JavaMavenProject. Name is XXX(whatever you want your project to be named). I have named mine as hibernatemaven. It will create a directory structure like below.



Looking at POM.xml (heart and soul of maven), shows



i.e. by default maven takes JUNIT dependecy to the project

Maven uses declarative dependency i.e. Maven itself take care of finding and downloading the required jars of the particular dependency you mention in POM.xml. The dependency resolution is iterative, maven will search for child dependencies as well.

Now when i added following to POM.xml



and made build the project in MyEclipse. All the dependencies of hibernate were added to maven dependencies (mentioned in the directory structure).

One can find the dependency xml entry from mavenrepository.com as i found hibernate dependency entry from http://mvnrepository.com/artifact/org.hibernate/hibernate-core/3.6.4.Final

In additiion to above maven has it repository on your local machine bydefault it can be found at.{user_home}/.m2

All dependency jars are referred from there only.

On the whole my first experience with maven was nice, until next time.

Wednesday, March 31, 2010

Spring SQLReturnType and varray

As a avid fan of Hibernate scheme of things i have been saved from JDBC junk/complexities for most part of my life as a programmer. As i say most of the times your biggest fears become reality sooner or later. So 5 yrs. down the i had to get out of the comfort of Hibernate and dig deep into JDBC calls for Multilevel collections.

Here i am talking about collections of collections (Varrays,Nested tables) in oracle.

Spring JDBC provide perfect foil to access these complex datatypes to java objects. Lets see how :

Remember the following checklist, this is must:

1.SqlReturnType : Interface to be implemented for retrieving values for more complex database specific types not supported by the standard getObject method. (Spring docs)

2.SQLData
The interface used for the custom mapping of an SQL user-defined type (UDT) to a class in the Java programming language. The class object for a class implementing the SQLData interface will be entered in the appropriate Connection object's type map along with the SQL name of the UDT for which it is a custom mapping.

3.http://forum.springsource.org/archive/index.php/t-10042.html (Thomas Risberg comments)

4.Mentions iNput and Output paramters in your StoredProcedureClass constructor.


public TestStoredProcedureDao(DataSource dataSource, String storedProcedureName)
{
super(dataSource, storedProcedureName);
declareParameter(new SqlParameter(INPUTPARAMETERID, Types.VARCHAR)); declareParameter(new SqlParameter(INPUTPARAMETERSETNAME, Types.VARCHAR));
declareParameter(new SqlParameter(INPUTPARAMETERDATEFROM, Types.VARCHAR));
declareParameter(new SqlParameter(INPUTPARAMETERDATETO, Types.VARCHAR));
declareParameter(new SqlOutParameter("extra",
OracleTypes.ARRAY,"EXTRA",new ExtraMapper()));
declareParameter(new SqlOutParameter("FROMDATE",Types.VARCHAR));
declareParameter(new SqlOutParameter("TODATE",Types.VARCHAR));
compile();
}


5.Suppose the structure of the EXTRA varray is as below:
Make sure all corresponding beans PARENT,CHILDINFO,CHILD1,CHILD2,CHILD3 implements SQLData and implement SQLData.readSQL and SQLData.writeSQL methods as :





public class CHILD1 implements SQLData
{

private String key;
private BigDecimal value;
private String sql_type = "CHILD1_TYPE";

public String getKey()
{
return Key;
}


public void setKey(String key)
{
Key = key;
}


public BigDecimal getValue()
{
return Value;
}


public void setValue(BigDecimal value)
{
Value = value;
}

public String getSql_type() {
return sql_type;
}


public void setSql_type(String sql_type) {
this.sql_type = sql_type;
}


public String getSQLTypeName() throws SQLException {
return getSql_type();
}


public void readSQL(SQLInput stream, String typeName) throws SQLException {
Key = stream.readString();
Value = stream.readBigDecimal();
}
public void writeSQL(SQLOutput arg0) throws SQLException {
}

}




Remember ExtraMapper class in the StoredProcedureClass mentioned above. It needs to implement SqlReturnType and use con.getTypeMap() to actually call for Driver methods implementation to map the DataBase datatypes to java objects.



private static Logger log = Logger.getLogger(ExtraMapper.class);

public Object getTypeValue(CallableStatement cs, int paramIndex, int sqlType,
String typeName) throws SQLException {

Connection con = cs.getConnection();
ArrayList extras = null;

Array array = cs.getArray(5);
ResultSet rs = array.getResultSet();
Map typeMap = con.getTypeMap();

try
{
typeMap.put("EXTRA_TYPE",Class.forName("com.ExtraBean"));
typeMap.put("PARENT_TYPE",Class.forName("com.ParentBean"));
typeMap.put("CHILD_TYPE",Class.forName("com.ChildBean"));
typeMap.put("CHILD1_TYPE",Class.forName("com.Child1Bean"));
typeMap.put("CHILD2_TYPE",Class.forName("com.Child2Bean"));

extras = new ArrayList();
while(rs.next())
{
ExtraBean tr = (ExtraBean)rs.getObject(2,typeMap);
if(null != tr)
extras.add(tr);
}
}
catch(ClassNotFoundException cnf)
{
log.info("Error"+cnf.toString());
}
return extras;
}

It works like a charm.

--
Pankaj Chaswal

Monday, December 14, 2009

Garils 1.1 1 and Hibernate Plugin 1.1.2 mis smatch

With the update of Grails 1.1.2 and Hibernate plug in 1.1.2. I ran through this error. As my application already was running on grails 1.1.1 so new hibernate plugin ran into compatibility issues.

Error :

plugin hibernate-1.1.2 requires version [1.1.2 > *] of Grails which your current Grails installation does not meet

As i was trying to update the plugins from Intellij idea. So it kept on downloading the greatest version of Hibernate plugin 1.1.2, even after i selected hibernate 1.1.1 in the drop down. So i tried this alternative way and it worked for me :


1. before doing a run-app, run following command

grails install-plugin hibernate 1.1.1

in the application root folder

then, once the plugin is installed, do a grails run-app

2. there is one more way for workaround to this issue.


download and bundle the hibernate-1.1.1 plugin within the application by adding something like:

grails.plugin.location.hibernate = './plugins/hibernate-1.1.1' to the grails-app/conf/BuildConfig.groovy


3. Third way is to upgrade your grails to 1.1.2


Hope this helps.

Thursday, May 21, 2009

Spring scheduling provides the easy configurable way for Cron jobs/ Schdulers.

1. myJobClass refers to the bean which refers to myJobClass.java (Actual job to be scheduled). UserDAO is injected to the MyJobClass. Spring scheduling is so far not in the picture i.e. loosely coupling

2. myBeanJobDetail is the bean which actually calls spring scheduling API. You need to set properties which are again Injected to spring beans on runtime.

3.Cron Trigger is the bean where you actually make your job a full blown trigger by setting up the triggering time.






Though this is a simple way of doing things but we can independently use Quartz API for scheduling. I will refer to that approach in my next blog.