Friday, February 01, 2008

CDMA

Confused Desi in Mainland America.
I admit. I really don't get it. Being a Desi in the states has brought its share of confusion in me. After much rumination I am sorry to say the confusion is not going to go away. What I have ascertained causes much despair as its neither going to change soon, nor is it going to be easy to see the light. We seem to be hard wired to certain ideas, belief systems and a way of thinking, what we call a mindset. Desi's in the US come in many flavors, the americanized ones who after an year of getting off the boat , all of a sudden seem to think that India is a village and crack jokes about it with his new homies in da 'hood [I call these the fresh dawgs ], or the more commonplace "OMG that coffee is 3 bucks .. thats like 120 Rupees. I'm NOT drinking THAT !" kind [the constant calculator], then we have the ever so common "Wanna screw white chicks, but I'll marry only a 'good girl' from India" type [the horny chauvinist ] and thats just a broad generalization.

Simon lives in an apartment and shares place with 3 other guys and cant find enough time to explain why India is still a frigid old village and how the American life has made him see the light. It was a Saturday night and like every other Saturday night he goes out to a club. Inside the club the American Desi faces a dilemma, the brown skins are outnumbered and his new found culture and lifestyle don't seem to stand a chance with the real thing. The first thing that he does is to scope out the place. Not for low hanging fruits but simply for prey looking the other side. The target is locked, he runs. Like an F-22 re-fueling in mid-air, he tries to connect his nozzle to the rear of his prey. The unsuspecting girl shrieks and jumps ! Simon engages the cloaking device called "run the hell outta there" and moves in to stealth mode. Bogie one is down. Between a couple of beers he hunts down another couple more targets. Mission accomplished, Simon returns to base before lights come on and blows his cover. At home he found the (single) bedroom locked, from the inside and yells "Sridhar, make it fast. I've got to sleep, gotta go to the church tomorrow"

Sridhar is working for an Indian software company and has been here for just about two months now on an on site assignment. He does not join Simon in his club quests since the entry fee is 10 bucks and thats like 400 rupees. Simon chuckles as an apparently exhausted Sridhar opens the door ... "dude ... get a life man. get a lap dance instead of a laptop. You stupid Indians ... break out of that shell man... taste the real life like me...". Poor Sridhar makes the mistake of asking Simon how the night went. "Dude ... cool man... I danced with at least 5 chicks man... I was grinding so hard with the third chick, I almost messed up my trousers man...hahaha...So you decided on what laptop you wanna buy ? " Sridhar's time once he got back from work was divide between searching for laptop deals, checking the USD to INR conversion rate every other minute and watching porn. "The Dell has a new coupon and I get it for 15 bucks less than the HP with the same config. But I'm still undecided if I want to go for a 2.0 GHz one and pay the extra 100 bucks. Or maybe I should go for that VAIO, it looks so sexy. I'd love to have that MacBook but nothing runs on it. Sim..."
Simon is heartily snoring by now lying next to Jinesh who was already dead asleep, and Sridhar ponders if wants to watch more porn or search for more deals, he starts by hitting finance.yahoo.co.in and checking the Dollar rate now. "Damn! down 15 paise again..."

Earlier that day Jinesh was out and his phone rings. "Hello Amma... ... Yeah I'm doing okay ma... How is everything there... okay... what ? the proposal ?... okay did you see the girl's photo ? okay... Is she fair ? how fair ? and did you like her ma ? .... Whats she doing ma ? what ? ... shes a fashion designer ? No ma... I told you na.. nothing but IT people please.. Its very hard to find job openings in these professions here, IT is easy and how do you think two people can survive here without a double income ? ... No ma... Plus you have absolutely no idea what goes on in these colleges ma... I'm sure she's had an affair and all... ... No ma ... I told you na... Look for only software engineers and girls who went to colleges nearby their homes, and not hostels... Okay ma... I'm in the midst of some work here, I'll call you back ma.. Yeah... I'm staying late in the office today, lot of work to do...Okay...bye." Jinesh puts the phone away and deftly slides a dollar bill in to Eva's g-string as she purrs in her Scandinavian accent "ready for your next lapdance... baby ?" somebody would have to wipe the smile off his face now.


But all this boils down to some simple facts. Simon has a confused identity, Sridhar, a confused bank statement and Jinesh, confused morality. But how should I explain myself as the guy who sat in the living room all the while just blogging about it ?

Thursday, January 10, 2008

Back 2 basics : JDBC Re-visited

We deal with numerous high level frameworks and abstractions in our day to day jobs, an sometimes we forget the basic basics. The little things that matter much. I found myself curling up with a think book only to spend hours, gain little and forget soon. So I started jotting down the stuff that actually make sense and pointers to brush the memory. No fluff ... just stuff.

Types of drivers
  • Type 1 : JDBC:ODBC bridge. These are to be used as a temporary solution to connect to databases and are inherently un-scalable and non-performant. they may also be feature limited.
  • Type 2 : Native API. These Drivers depend on native libraries to communicate with the DB. They are basically a thin Java wrapper around the Native code using JNI. They perform better than Type 1 but the portability is a problem as the native library may not be available for certain platforms.
  • Type3 : Net Protocol, they are fully Java drivers that expose the DB operations in a net friendly way. They also perform well and the portability is not much of an issue since there is no client side installation.
  • Type4 : thin drivers. These are 100% Java drivers and communicate with the DB using the DB's native protocol. They perform the best nowadays and being 100% Java they are very portable.
Choosing a driver depends on the situation the applications scalability requirements, cost and performance expectations. as a rule of thumb, Type 4 drivers are the default choice. as nowadays most DB vendors provide one for free and they perform really well.

DriverManager and Connections

You can register a driver by
  • Creating an instance of the Driver (impl) class , and using DriverManager.registerDriver()
  • Setting the property jdbc.drivers (pass -Djdbc.drivers=)
  • Loading the driver class (Class.forName()).
The last one is the most poular as this lets you keep the Driver's name as an externalized Property(eg : in a Properties file). For any DB operation you need a Connection to the DB. This can be got by simply calling DriverManager.getConnection(url, username, password).
The URL is the JDBC URL to the DB, and the user name an password have to be valid to get a connection.

Connections are limited and are heavy weight objects. Creating a connection requires several round trips to the DB and is very expensive, hence they have to be reused as much as possible so we have Connection Pooling. Always close connections. If you see unexpected SQLExceptions that complain about not having a connection, then try to trace out if you have connection leaks.

DataSources have the capability for Connection Pooling and is usually configured in a vendor specific way. DataSources are mostly accessed though a naming service like JNDI.


Application1 / app server
create a datasource using the vendors wway of doing it.
bind the DataSource to a JNDI context under a name.
Application 2 / client app
lookup the DataSource from the JNDI using the name used to bind it.
call getConnection on the DS.

Statements and ResultSets

There are 3 flavors of Statement objects and they are basically a pipeline to send an SQL statements to the DB and get results back in a ResultSet object. All the statement objects re obtained fro the Connection object.

Statement is the basic type and represents a dynamic SQL statement. Its the parent of PreparedStatement. each time you execute a Statement, the DB has to do a hard parse on the SQL and execute it. This can be poor for frequently executing SQLs. On the other hand the creation overhead for a Statement is considerably smaller than PreparedStatement.
PreparedStatement represents a pre compiled SQL statement. These statements are created with placeholders denoted by '?' where you can substitute values. the first time the PreparedStatement is executed the DB precompiles it and a hard parse is not necessary to subsequent executions. this improves the performance for frequently executed SQLs abut the creation or “preparing” overhead is more than a Statement. Some database driver like Oracle driver can use numbered placeholders than the generic '?'
The '?' are given values and they are indexed starting from 1

Callable statements are used to execute stored procedures and the they have the capability to return output parameters for the stored procedure. The in parameters are set using the setXX methods and the out parameters are registered using the registerOutParameter method. as with other types of Statements the indexes for the placeholder '?' starts at 1. INOUT parameters are handled by using the same index to set the IN parameter and registering the OUT parameter.

Remember that ResultSets are connected to their Statement objects which are connected to their Connection Objects and the ResultSet represents an open cursor in the database, so failure to close the ResultSet and the statements will lead to the DB eventually running out of cursors. Also you cannot have more than one open cursor associated with the same Connection (some drivers will throw SQLExceptions) , If you need to re use a Statement object, save the result set in a vector or a list first.

All these classes will have the execute, executeUpdate and executeQuery methods
executeUpdate is used to send a DML like INSERT, UPDATE,ALTER or EXEC. They do not return database rows, and hence the return type for this method is an int, that gives the number of rows that were affected by the SQL. in case of ALTER or EXEC , the return will always be 0, since the SQL will not affect any rows.

executeQuery is used for executing a Select statement and the selected rows are returned in a ResultSet.
The execute method is generic and can be used for both types of SQL. But execute is slower than the other two. Also execute returns a boolean , true if a ResultSet was returned and false if an update count is waiting. use getResultSet or getUpdateCount to get the relevant info. Generally its better to use the specific methods, as they perform better and require lesser coding.

The fetch size and Fetch direction can be provided to a statement to optimize the query, but this is db dependent, and the Driver may even choose to disregard and make decisions on its own.
a ResultSet is always pointing to “before the first row”. the first row is obtained by calling next() on the ResultSet . the call to next returns false if a row is not available
JDBC 2.0 adds the ability of random access to ResultSet . Its controlled by setting ResultSet.TYPE_FORWARD_ONLY (forward only iteration) ResultSet.TYPE_SCROLL_INSENSITIVE (is a snapshot of the results) ResultSet.TYPE_SCROLL_SENSITIVE.(is sensitive to the back end changes to data)
JDBC 2.0 defines updatable rows. to update a row, the ResultSet should be scrollable and the underlying SQL refers a single table. This is referred to as ResultSet concurrency.
ResultSet.CONCUR_READ_ONLY – read only ResultSets .
ResultSet.CONCUR_UPDATABLE – updatable ResultSets
Inserting rows are a 4 stage process that inludes a staging row.
move to the staging row ( rs.moveToInsertRow() )
update the staging row ( updateXXX )
insert the staging row ( rs.insertRow )
move to the new Row (rs.moveToCurrentRow)
JDBC 3.0 adds ResultSet holdability that determine if the DB cursor is held even after a transaction has committed or if the cursor is closed on a commit. these can be specified when a connection is created.
ResultSet.HOLD_CURSORS_OVER_COMMIT – holds cursors and the ResultSet is usable even after the commit.
ResultSet.CLOSE_CURSORS_AT_COMMIT – closes the cursorr at a commit.
extraction of data from the ResultSet is using the getXXX methods and you can either specify the column indexes(starting at 1) or column names(case in-sensitive)
The ResultSetMetaData object gives details about the table and columns, like the column count , the column names and the tale name. Always remember the resource utilization in jdbc. Result sets cache data locally and they may re-query the db if it needs to.

Transactions and Batch Processing
The most primitive way to control transactions is to use the setAutoCommit method. enabling auto commits forces a database commit after each operation. Its usually best to set this to false and deal with commits , though its more coding. It gives more control and often better performance. But remember to invoke the commit method once the db operation is done and rollback if an exception was caught.
Batch processing is essentially like transactions, but they are limited to a single Statement. use the method Statement.addBatch() and executeBatch()

RowSets are a new spec and they can be seen as ResultSets that can be used like JavaBeans. Some kinds of RowSets can be disconnected, ie, they cache all the data and they need not be backed by a live connection to the database, making them ideal to encapsulate data and pass them around.

Q&A

  • How to find the number of rows returned?
if the ResultSet is scrollable, move to the last row and call getRow, which returns the row number.
In most cases a better soln will be to issue a second SQL with a count(*).
ResultSet navigation does not seem to work.
if using JDBC 2.0 navigation methods , make sure that the driver supports the methods.

  • Why is it unsafe to pass Resultset objects around?
the ResultSet objects are backed by the Statement and Connection objects. if they go out of scope or get closed, the ResultSet cannot get access the data., except for the data it cached. Also since they represent open cursors on the database, passing the ResultSet objects around causes the database to run out of cursors.

  • What are invalid cursor state error messages?
you get these when the ResultSet is out of a valid range., like trying to get data from a ResultSet before calling next(), or when the ResultSet has scrolled beyond last row.

  • How to handle nulls?
database nulls are different from the Java 'null' value. DB nulls for an INTEGER column may be converted to 0. To properly handle this, get the data with the getXXX method and then invoke the wasNull() method on the ResultSet. The method works only after the data has been extracted from the ResultSet.

  • How to handle BLOBS and CLOBS ?
BLOB requires java.sql.Blob but CLOB is provided by java.sql.Clob. You get data as raw bytes, with a BLOB, and characters in a CLOB
the new MySQL and Oracle 10 drivers provide the getString() method that can return the whole CLOB .

  • What is the best collection class to store ResultSet data ?
ArrayList or LinkedList.
ArrayList has a constant 'average' cost for appending to the end of the list as the backing array may sometimes require re-allocation. Linked lists have the constant cost.
insertions in random locations are better with LinkedLists as the cost is constant. ArrayLists require to shift the following elements.
ArrayLists incur space overhead in the form of reserve space at the end of the array. Linked Lists incur even more space overhead for Entry objects(this is per element).
Depending on the usage, a Map may be a relevant too.

Wednesday, November 07, 2007

The American Shopping List

So you're happy and overjoyed at moving to the US and stuff, but wondering what to bring ?
I could never find a shopping list that did justice to Indians, anywhere on the net so heres a list of things to pack when you get on that plane from India to the US.

Assuming : You are probably allowed 2 pieces of check-in luggage of 32 kilograms each.

What to Buy
-----------
Two big and strong suitcases, that can hold 32+kgs and can take quite a beating, as these would be tossed around by many hands.
I prefer Hard-Top suitcases, but soft tops would do just fine, as long as they are sturdy enough. The total dimension of the suitcase would be mentioned by the airline and it varies. check with your airline(call thier airport office and ask).

One smaller suitcase, this is hand baggage. make sure this is just the right size so that you can carry some stuff. again check max dimensions with airline.

Passport wallet. Buy a passport bag/wallet or a small bag that you can hang from your neck safely, for the passport and a bit of cash. Use this for putting things you need frequently during the flight.

A college-bag/backpack. This comes in handly once you are here, so get one from india if possible. Its better to pack this inside the checkin/hand baggage, so that it does not become an extra piece of baggage.

Dollars. get at least 2000 bucks from the nearest bank. get 1500 and travelers cheques(note the numbers down and keep them separately, in case you get robbed, the cheques can be canceled if you have the numbers). 500 dollars in cash. get at least 100 bucks in small notes(less than 50) and change.(banks might not have small notes or change, visit relatives or something ?)

The important stuff -
- All your certificates, educational and employment related documents.
Tax stubs(form 16), pan card, and photocopies
Investment records in India
The visa and immigration documents
Offer Letters, LCA, educational evaluation, i797 approval notice
drivers license, and any other photo ID you have.
credit cards and bank info, including statements for the last 3 months
(The originals go in the hand baggage, one set of copies go in each of the suitcases)


Utensils-
Okay, chances are that you are not going to cook here(at least initially), but in case you do try to get these.
A small pressure cooker(smallest you can find)
Two vessels for cooking, medium size, with at least one lid.
A plastic tumbler
A plate(melamine ware)
spoon and a fork
A big spoon for curries, and a wooden spatula

Food stuffs

Do not carry much food stuffs. most of the stuff you can buy here. only carry what you feel is most required.
Pickles
chutney powder
any such preservable stuff. do not carry raw food/meat/veggies. Or curries/cooked food. they will be thrown out by the authorities.

Supplies
Slippers, light weight ones
a good pair of sandals
sneakers
black formal shoes
black / brown everyday casual shoes(leather)
wallet. buy a good wallet that has enough spots for credit cards and cash.
Belt. buy at least 3-4. buy the expensive ones, like bullchee or peter england. (you'll know what expensive means once you get here ;) )
spectacles, buy 2 spare specs and get a written presciption. visiting a doc for a prescription is very expensive.

Toilette(try to cut down on weight here)
Soap(just one please!)
Soap dish
a small mirror
comb/hair brush
shaving gel, small tube
razor
toothbrush
toothpaste
hair gel, if you have hair.
A good perfume/body spray(or more , its expensive here)


Clothes
Buy a good blanket,(the one thats not wool but feels like it, its thick but light.)
bring lots of socks. (LOTS)
undergarments(the more the merrier)
Thermals (buy the ones with long sleeved t shirts, about 4 pairs seem sensible )
Shirts, as you please
trousers , as you please
Ties , bring 3
Suit. Just one, really good formal suit. Go to raymonds and they'll fix you up.

Medicines
Visit a doctor. tell him you are travelling, and he'll give you a better list.
Always get a prescription. Its very important here, without a presciption, you cannot have medecines here.
Buy any medicines you regularly take , for any condition you might have.
General Medecines :
Crocin a couple of strips
1 small bottle cough syrup
medicines for vomiting, a laxative, and something to inhibit loose motion.

Utilities
Scotch Tape
Hangars, plastic, a dozen
A portable hard disk, with all your data
i pod/Walkman if you are inclined that way
An international credit card with at least 40-50 k credit limit(it'll take time for you to get a credit card here and sometimes they are required.)


General
Have a skype account and set it up at home so that you can communicate easily with your family. Teach them how to use it.
Get to the airport early. airports may have delays.
When checking in you baggage, be very careful. if you have two pieces of check-in baggage, make sure that they put bag tags on both of them
make sure that the corresponding bag tag counterfoils are stuck on your boarding pass.
cross verify this that the bag tags on your boarding pass match the ones on the baggage
baggage stealing is a very common thing in India and this happens frequently.
always have your passport, a copy of your DS-156,157 in hand(not in your hand baggage, you will need to use this in the flight).
use the passport wallet or a hip pouch for this.
always have a pen with you(you will need to fill out immigration forms during the flight).
try to sleep as much as possible during the flight.
before boarding its a good idea to have a peg or two. not more. It helps you sleep.
if drinks are offered on the plane, have it. helps you doze off again.
have food. the timings will be awkward and you wont be hungry when they serve and they wont serve when you are hungry. live with it.
during the last leg of the flight you will be offered forms to fill out for the immigration procedure
you need to get the form called I-94 . Once you get here, this piece of paper is more important than your visa or your passport.(this proves that you are a legal alien in the country, and gives the date till when you can be in the country legally.)
The name you write on this is important as that will be the official name from now on.(make sure you get the first,middle and last names right on this.)

Once you get here
Mostly everything will be(should be) done by your employer.
Its good to carry small change with you, for public phones if you have to call the employer to pick you up or something. (don't ask me where to get small change, buy a coffee from the airport or something)
If you take a taxi to the place where you are going, give a 15% tip to the cab driver(15% is the minimum) and ask for a receipt.
sleep as much as possible.
Open a bank account asap. you can do it with just your passport. the big banks here are bank of America, chase bank, wachovia, PNC etc.
You might need to buy a laptop. I leave this to you, dell sells only online, and the big electronics shops here are BestBuy, CircuitCity, CompUSA etc. the really good ones cost money, but keep in mind that if you just plan to use it for email, reading books, watching movies then you dont need such a good one.(I made this mistake). check the site www.deals2buy.com before you make the purchase, they'll have the deals listed.