Tuesday, February 20, 2007

Why Prepared Statements are important and how to use them "properly"

Prepared Statement Cache is available as a standard feature of app-server. This feature has to be used judiciously. It work faster only when the particular statements are executed multiple times in the same transaction.

Weblogic
Cache Prepared and Callable Statements
When you use a prepared statement or callable statement in an application , there is considerable processing overhead for the communication between the application server and the database server and on the database server itself. To minimize the processing costs, WebLogic Server can cache prepared and callable statements used in your applications. When an application calls any of the statements stored in the cache, WebLogic Server reuses the statement stored in the cache. Reusing prepared and callable statements reduces CPU usage on the database server, improving performance for the current statement and leaving CPU cycles for other tasks.
Using the statement cache can dramatically increase performance, but you must consider its limitations before you decide to use it.
Websphere
The WebSphere® Application Server data source optimizes processing of prepared statements. It manages a pool of database connections, as well as an associated cache of prepared statement objects. Prepared statements are cached separately for each connection that executes them.



Why Prepared Statements are important and how to use them "properly"

The top Java EE best practices

IBM WebSphere Developer Technical Journal: The top Java EE best practices

Tuesday, January 30, 2007

Software Re-Useability

This is the best response
Most folks think of it in terms of some kind of "mother of all objects" thing, whereas the mentality to code with re-use in mind should be enabled on a daily basis on the lowest level. Don't dream of creating a reusable component that will save the world. Start with creating microscopic reusable code snippets to get you daily job done. That, at least, will make you a better programmer, and your code will be better, your applications will be more flexible, configurable, and easier to maintain. Once that is in your blood, then, perhaps, one day you will be indeed capable of conceiving solutions like Spring, etc.
http://www.theserverside.com/user/userthreads.tss?user_id=211920

Software Reusability: Myth Or Reality?

Software Reusability: Myth Or Reality?

Thursday, January 04, 2007

Moving from cvs to svn step by step

Step 1: Install your favorite Linux server. I used Fedora 6. Fedora 6 comes with svn server 1.4 . Name is F6

Step 2: Download cvs2svn  from http://cvs2svn.tigris.org

Step 3 : As root, run 'make install'.

Step 4: Create a regular user on your server. I created svnscm

Step 5 : Login as svnscm , Copy the CVS repository from the cvs server to F6. I used "scp -r " 

Step 6 : As svnscm cvs2svn --dumpfile DUMPFILE CVSREPOS . svs2svn performs 9 pass process on your cvsrepository (CVSREPOS). After successful execution it creates a DUMPFILE. I used cvs2svn --dumpfile cvsdump.dmp myproject.

Step 7 : Create svn repository  svnadmin create /usr/local/svn/repos.

I used "svnadmin create /source/repository/myfavoriteproject"

Step 8 : Load the dump file into your new svn repository. svnadmin load REPOS_PATH

I used svnadmin load /source/repository/myfavoriteproject < cvsdump.dmp 

Step 9 : Verify the svn repository created. I created a folder /home/svnscm/workspace. 

"svn checkout  file:///source/repository/myfavoriteproject/trunk [Note : trunk in the url ]

Accessing svn from a remote machine

Step 10 : Verify port 3690 is not blocked

Step 11 : In your svn client use this url svn+ssh:///source/repository/myfavoriteproject/trunk. [ssh is by default enabled in F6]. I used  TortoiseSVN 1.4.1 for windows

Step 12 : Add pre-commit hooks and post commit hooks That's it .

Its party time enjoy your svn

Additional Notes

  • svn creates trunk, branch, and tags as separate folders. If u checkout myfavoriteproject it will download tons of files.
  • When you add more developers to your project access control can be handled at file permissions level. Further fine tuning can be done in svnserv.conf. This file is available in each repository

Ref Book on Subversion

Sunday, December 17, 2006

Smart Key Generator ejb3


Java Persistence provides facility for auto-generation of primary keys. But limited to integer datatype. Further there is no convenient way to access these generated values. Its often needed that the primary key is a combination of identifiable data. Such keys are also known as Smart Keys.
Smart Keys also need automatic serial number generation.
Following solution uses HiLo sequence generation algorithm.

The solution contains one Stateless Session Bean and a Singleton. Additionally it consists an Entity (JPA)


package jkook.vetan.utils;
/*
* HrSequenceFacade.java
*/
@Stateless
public class HrSequenceFacade implements HrSequenceFacadeLocal, HrSequenceFacadeRemote {


@PersistenceContext
private EntityManager em;

private UIDDispenser uiddispenser;
private static final Logger log = Logger.getLogger(HrSequence.class.getName());

/** Creates a new instance of HrSequenceFacade */
public HrSequenceFacade() {

}

public void create(HrSequence hrSequence) {
em.persist(hrSequence);
}


public void edit(HrSequence hrSequence) {
em.merge(hrSequence);
em.flush();
log.info("HrSequence Updated");

}

public void destroy(HrSequence hrSequence) {
em.merge(hrSequence);
em.remove(hrSequence);
}

public HrSequence find(Object pk) {
return (HrSequence) em.find(HrSequence.class, pk);
}

public List findAll() {
return em.createQuery("select object(o) from HrSequence as o").getResultList();
}

/*
if dispenser is null
create new dispenser
getnextHi from dispenser

*/

@TransactionAttribute(TransactionAttributeType.REQUIRED)
public Integer getNextID(String entityname) {
log.info(" getNextId called");
uiddispenser = UIDDispenser.getDispenser(em);
return uiddispenser.getNextID(entityname,em);
}

public Integer getCount() {
Query q = em.createQuery("select count(o) from HrSequence as o ");
Long count = (Long)q.getSingleResult();
if(count != null){
return count.intValue();
}
return null;
}

public Integer getCurrentID(String entityname) {
uiddispenser = UIDDispenser.getDispenser(em);
return uiddispenser.getCurrentID(entityname);
}
}



/*
* UIDDispenser.java
*
*/

package jkook.vetan.utils;

public class UIDDispenser {


private static UIDDispenser uiddispenser;
private Map allentries;
private static final byte maxLo = 25;
static Logger log = Logger.getLogger(UIDDispenser.class.getName());
/** Creates a new instance of UIDDispenser */
private UIDDispenser() {

}

private UIDDispenser(EntityManager em){
// populate all entries
Iterator i =
em.createQuery("select object(o) from HrSequence as o")
.getResultList()
.iterator();
// for large database initiate the hashmap with count of HrSequence
allentries = new HashMap();
while(i.hasNext()){
HrSequence hs = i.next();
hs.setNextHi(hs.getNextHi()+maxLo);
allentries.put(hs.getEntityname(),hs);
System.err.println(" enityties +++ "+hs.getEntityname());
}

}

public static synchronized UIDDispenser getDispenser( EntityManager em) {
if (uiddispenser == null)
return new UIDDispenser(em);
return uiddispenser;

}

/*
* get nextLo
if lo > max_lo getnext HI
set lo=0
return HI+Lo
*
*
*/
public synchronized Integer getNextID(String entityname, EntityManager em){

HrSequence hs = allentries.get(entityname);
if(hs.getLo() >=maxLo){
log.fine(" LO Val is > maxLo " +hs.getLo() + " maxLo is " + maxLo );
hs.setNextHi(hs.getNextHi()+maxLo);
em.merge(hs);
byte b=0;
hs.setLo(b);
log.fine("Updated HI_VAL");
}
return hs.getNextHi()+ hs.getNextLo();
}

public Integer getCurrentID(String entityname){
HrSequence hs = allentries.get(entityname);
return hs.getNextHi()+ hs.getLo();
}
}

Usage :
@EJB HrSequenceFacade hrSequenceFacade
{
....
....
// start of smart key generation
// smartkey part1
// smartkey part2
Integer nextSerial = hrSequenceFacade.getNextID("NameityOfTheEnt")
// primaryKey = smartkeypart1 + part2 + nextSerial



Ref : Using Primary Keys with Java Persistence

Friday, December 01, 2006

Choosing Primary Keys

Exert from OracleFAQ

  1. The candidate key must be unique within its domain (the entity it represents, and beyond, if you also intend to
    access external entities).
  2. The candidate key can not hold NULL values (NULL is not zero. Zero is a number. NULL is 'nonexistent value').
  3. The candidate key can never change. It must hold the same value for a given occurrence of an entity for the lifetime of that entity.

BizSutra adds

PrimaryKey has to be HUMAN Readable

There are many strategies for identifying a Primary Key. With advancement of OOP the debate for surrogate keys has elongated. The fact remains that the strength of Object Oriented Programming is readability of the code. This capability has to be extended to persistence layer. Using surrogate keys defeats the above capability. Having Mnemonic code in foreign key fields is a good practice. Gurus suggest is 8.

Data Live Longer Than the Logic Accessing the Data