Friday, May 14, 2010

Varargs methods

Varargs methods have been introduced in Java 5, but they currently don’t have a wide diffusion and most part of the Java developers are not aware of their existence.

The vararg notation allows methods to accept a variable number of arguments and packs them into an array. This notation is not as convenient as you might like, because the arrays it creates suffer from the same issues involving reification as other arrays.

A Varargs methods is a standard Java method that can be invoked using a different number of objects as parameters. Example:
 // Method declaration  
 public void print(String... strs) {  
      for (int i = 0; i < strs.length; i++) {  
           System.out.println(strs[i]);  
      }  
 }  
 // Method call  
 print(null, "Hello", "World", "!");  
The syntax "String…" stands for "any number of objects of type string". You can manage this “strange” object as a normal array inside the method body. From the method writer point of view, a Varargs method is just a method with an array as its last argument. The real difference between this method and a standard one becomes evident when it is invoked. Note that the first argument is ignored in the declaration of the print method and if you desire you can write a method using just a Varargs parameter and nothing else.
The compiler translates
 return arithmetic.add(x, x, x);  
to
 return arithmetic.add(x, new Object[] {x, x});  
when the type is given in the method call. It is, as if it was doing the translation before making the decision of the T type.
 int testMethod (int x, int y, int z, int ... n) {}  
A method can accept "normal" parameters along with variable length parameter lists. To do this you must ensure that the variable length parameter list is declared last. Also, a method can't declare more than one varargs parameter.
SYNTAX:
 methodName (type t1, type t2, type t ... arguments) {}  
NOTE: Statements/Snippets taken from multiple posts purely for my understanding as well as jotting different experiences at a single place.

Thursday, May 13, 2010

Spring Transaction Management Facts

Spring Transaction Management Facts: (we may call it as business transactions as opposed to db transactions):

1. One of the most compelling reasons to use the Spring Framework is the comprehensive transaction support. Without a transaction, Spring can use a different connections between the two queries back-to-back.

2. Transactions are declared on the method level, but are actually bound to the running thread. It means that once you declare a method as transactional that transaction can span a called method tree of unlimited depth. That's why DAO layer is, normally, not declared as transactional.

3. Transaction is bound to the thread once you start a method declared as transactional. It stays bound to the thread until the method is finished or transaction is suspended. Non-transactional methods do not suspend transactions. Any method in the call tree that knows how to lookup the transaction bound to the thread can do it, no matter if method is marked as transactional or not.

4. Spring does not change autocommit mode or execute commits or rollbacks or do anything to your connection unless explicitly told.

5. Transactional behavior cannot work when autocommit is on.

6. Spring has capability of rollback the transaction in case of checked exceptions also but for that we need to configure the spring beans.xml accordingly.

7. For more details, please refer the link:
http://static.springsource.org/spring/docs/2.0.x/reference/transaction.html

NOTE: Sincere thanks to all the posts that I referred to when I implemented, experienced the above important points.

Monday, May 10, 2010

Database Basics

Something everyone wants to have quick access to ....

1. Catalog: A relational database contains a catalog that describes the various elements in the system. The catalog divides the database into sub-databases known as schemas. Within each schema are database objects -- tables, views and privileges.
The catalog itself is a set of tables with its own schema name - definition_schema. Tables in the catalog cannot be modified directly. They are modified indirectly with SQL-schema statements.
2. Schema: It is a collection of named objects used to provide a logical classification of database objects. It may contain objects such as tables,
views, aliases, indexes, triggers, and structured types.
3. Table: It is a logical structure maintained by database manager which is made up of columns and rows.
4. View: It is a parsed SQL statement which fetches record at the time of execution. It may be thought of as a virtual table that doesn't really exist in its own right but is instead derived from one or more underlying base tables.
5. Alias: The alias names are local synonyms given to certain database object.
6. Index: It is a type of data structure that allows for (potentially) faster access by providing the database with quick jump points on where to find the full reference (or to find the database row).
7. Function: It is a subprogram written to perform certain computations and return a single value.
8. Stored Procedure: It is collection of SQL statments compiled as a program which reside in the database.
9. Trigger: It is procedural code that is automatically executed in response to certain events on a particular table or view in a database.
10. Synonym: It is an object in Oracle that basically allows you to create a pointer to an object that exists somewhere else.
11. Sequence: It is an object in Oracle that is used to generate a number sequence.
12. JOIN: Return rows when there is at least one match in both tables.
13. LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table.
14. RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table.
15. FULL JOIN: Return rows when there is a match in one of the tables.
16. SELF JOIN: Querying for the result set of join with the same table.