Pages

Showing posts with label simple java mistakes. Show all posts
Showing posts with label simple java mistakes. Show all posts

Sunday, October 21, 2012

mistake when injecting null value to a variable or construct

Say you need to inject null value to a property. How would you do it? Let me guess.

<bean id="youclassid"class="yourclass">
        <property name="distance" value="null" />
</bean>

Sorry to disappoint you. The above code will not work. The correct way is shown below.


<bean id="youclassid"class="yourclass">
<property name="distance"  /><null/></property>
</bean>


Monday, September 3, 2012

Spring AOP Error : Cannot Proxy Target Class Because CGLIB2 Is Not Available error in spring

This is a common error in spring development

The solution is

put the cglib.jar as a library to your project. you can obtain this from Cglib official web site
http://cglib.sourceforge.net/

Sunday, September 2, 2012

spring transaction management


recently I wrote a spring code.

JdbcTemplate insert = new JdbcTemplate(dataSource);

insert.update("INSERT INTO item (price, item_category) VALUES(?,?)",new Object[] { beverage.getPrice(), beverage.getItemCategory() });

int id = insert.queryForInt( "SELECT last_insert_id()" );

System.out.println(id);return insert.update("INSERT INTO beverage (id, name, quantity,size) VALUES(?,?,?,?)", new Object[] { id,beverage.getName(), beverage.getQuantity(),beverage.getSize() });

How ever the id was always 0. There reason has been,

whole code above must be wrapped in a transaction. Otherwise JdbcTemplate can use a different connection from the pool for all statements and last_insert_id() is tied to a transaction.
Threfore @Transactional can be used to avoid this error.

How ever this solution can be applied only to non MyISAM type of tables. Because MyISAM type does not support transactions.

Sunday, August 12, 2012

Java UnsupportedClassVersionError

This exception is most likely to be thrown when you compile the program from one version of java and running it on another version of java.
Eg : You compile the code using jdk 7, 6 etc... and run on version 4.

Tuesday, July 17, 2012

Jdbc driver time out.

Recently I deployed an app on glassfish and it worked really well for about half a day and then it threw an exception each time the service was called. Following is the excetion

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No 
operations allowed after statement closed.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance 
(NativeConstructorAccessorImpl. java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Deleg
 
Later found out that the jdbc driver connection automatically 
expires after 12 hours.  Setting conncetTimeout to 0 in the 
jdbc connect url as a parameter resolved the problem.

java split with "."

String a = "a.jpg";
String str = a.split(".")[0];
This will throw ArrayOutOfBoundException
Why is this?
This is because "." is a reserved character in regular expression, representing any character.
Instead, we should use the following statement:
String str = a.split("\\.")[0];
When the code is compiled, the regular expression is known as "\.", which is what we want it to be