Skip to main content

Posts

Showing posts with the label jee

MySQL > java.sql.SQLException: Error writing file '/tmp/MY1kynpA' (Errcode: 28)

Recently, a heavy burst of traffic into our production webservice caused this error. As with all Mysql errors, this shows little information. Like any sane person would think, we checked if the filesystem was full. We found the /tmp has 98% free space. Permissions are all fine. No locks on table. Increasing /tmp partition - NoGo. Just crazy error message. However, the issue was beating us time and again. It took us a couple of days and we realized the cause was a select query. The query was fetching from a table of about 2 million records. Although it was fetching a limited subset, the column in the order by had no index. This was causing mysql to load records into pagefile in tmp for sorting, apparently. Created the index on the sort column seemed to have solved the problem. We also augmented it with the following options in my.cfg sort_buffer_size=2M table_cache=1800 thread_cache_size=384 tmp_table_size=64M

java turorials: Learn everything Java basic to enterprise

Java Programming basics Introduction to Java programming   ServerSide Java Download Tomcat Demo - Servlets and JSP JSP Tutorial IBM JSP tutorial   Enterprise Java JEE 5 tutorial Develop Web services with RAD Part1 Part2 Crash course in Enterprise JavaBeans 3   Struts IBM Tutorial Apache Wiki (List of Tutorials)   Spring Viral Patel’s turotial Reference Documentation   Ibatis JavaLobby Introduction Apache Tutorial   Hibernate JBoss Tutorial    

Hot Deploy resource file in Spring.

The general way to read a Resource file in Spring is something like this new ClassPathResource(pathToFile).getInputStream() However, this will cache the contents of the file (stream) in the class loader. Although this is not a major problem, there may be cases where reading the file fresh everytime (Hot deployable file) is required. That can be achieved with the following code new FileInputStream(new ClassPathResource(templateFile) .getFile().getAbsoluteFile()); This will gives cache free control on the file and its content. (Important) Note : * Doing the AbsoluteFile way, will always read the content of the file. So if it is being done on a High frequency, A Managed Caching layer must be considered * AbsoluteFile call will NOT work for resource files inside jars, they have to be in classpath folders (e.g.WEB-INF/classes)

Google App Engine / Java / cold Starup benchmarks (issues).

This is connected to the post in Google App Engine Group .  Although Google Appengine gives out a java container, I had to roll back to the python code I used for version SarathOnline 3.1 – for a sole reason – perfromance. My (blog) site has a very low traffic (compared to what google thinks a regular traffic application). Averaging ~100 visits/day. All the css and js is dynamically generated. My current python code is doing pretty well servicing these requests @ a maximum of 2.something secs

Sun Tech Days – My Day 2

Day 2 missed James. But the tech series remained just as educating as Day1's. I dug deep into the JEE6 foundation laid by Yesterday's  sessions. Today: I Jugged in knoweldge from Java Persistence API 2 Overview of Servlet 3.0 ZFS (a Whole lot on ) Context and Dependency Injection in JavaEE 6 Java EE 6 Tools Show: NetBeans, Eclipse, IntelliJ I have to bold all of them, but the ZFS really instigated me to push OpenSolaris into my dev space. Look forward to some OpenSolaris entries coming.

Been There (Done That)

Finally, I been to Sun Techdays – here in Hyderabad. Now I can say.. "Been there".. done that!.. But it was an amazing experience, with more than 1500 people in HICC, Novatel, Hyderabad. Best of all, I was able to see James Gosling. Live. In Person. They were very exhaustively educating 9 hrs. Although I was keen on Enterprise Track, I was able to sneak some time in JavaFX and Solaris Technologies too. I stuck myself in: Java EE 6 & GlassFish v3: Paving the path for the future. Designing and Implementing Secure RESTful Web Services. Hands On Lab: Build End-to-End RIA Applications Using JavaFX, Dojo, REST, JPA, and MySQL. Glassfish and JEE 6 Features (Stall at the exhibition). OpenSolaris Installation desk (Okay, I got OpenSolaris on  my vbox). Enterprise Java Persistence with Oracle TopLink. OSGi & Java EE in GlassFish. I loved ArunGupta 's talks about Glassfish. Especially, the OSGi one. Overall, its was a fascinating experience. I will loo...

Spring Framework 3.0 GA released!

SpringSource announced its GA release today. My personal reasons to "go for it" - REST and Huge improvements in Annotation support. It will be interesting to see how this release is accepted by the community. 2.5.6 seems just yesterday. People (like me) with roots (and lot of existing applications (with issues )) in Spring 2 are just getting their feet wet with migration to 2.5.6 and the magical world of annotation based configuration. Now, I have to calculate ROI on migrating my still-in-development applications (on 2.5.6) to 3.0. ah. I am so in a jinx.

Error creating bean with name 'tilesConfigurer'

While building a skeleton web app, using Spring and Tiles, I encountered this strange error. I am trying to configure Spring 2.5.6 and Tiles 2.2.1 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tilesConfigurer' defined i n ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.UnsupportedOperationException: Class org.apache.tiles.web.util.ServletContextAdapter not recognized a TilesApplicationContext at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) at java.security.AccessController....

Error handling and Production

When you are developing a customer front web application, you should take every measure to hide technical details from the user. This is especially true for JEE applications. Not only because the user is pissed off looking at a huge stack trace but also it reveals a lot. Look at the following figure This famous shopping site has been developed to show stack trace on the web page. The developer cleverly hid the trace when the page loads up. But leaves a link that uses DHTML to show the hidden div. That kinda defeats the whole purpose. If the user sees this link, he is less likely NOT to click it. If you really want to have a handle on this. Print out a unique token (a timestamp, md5 hash or something - get creative here), and correspondingly log it in your appserver along with stack trace. Have the user (customer or your pain-in-the-neck-QA guy) email you that token. Now you can grep that log for it. Now, you know, I have less confidence in that site this day forward.

iBatis SQL with dynamic like operator

In iBatis, a parameter is escaped, autoquoted and replaced automatically. So a #stringParam# will be auto autoquoted and then replaced. Its also escaped, meaning symbols like ' and % are escaped. This causes a problem when you have to do wildcard searches. With a like operator. Say, You have to look up employees by first name. The SQL would look like select * from emp where first_name = 'sarath' and the iBatis Query would be simply <select id="getEmpByFName"> select * from emp where first_name = #value# </select> On the same lines, A wildcard search of firstname would be (in SQL) select * from emp where first_name like '%sar%' But, if you just make an iBatis select like: <select id="getEmpByFName"> select * from emp where first_name like #value# </select> and make it concatinate with "%" before passing it to queryForList(), You will see nothing in results. This is because iBatis will escape % too. ...

Write iBatis dynamic query that returns results only when atleast one parameter is sent

iBatis allows building dynamic query conditionally based on properties sent to the named SQL. Suppose We have a Person implementation where the search page allows search by id or by email id (assuming both are unique). We could write two queries <select id="getPersonById" resultClass="Person" > select * from PERSON where ID = #value# </select> <select id="getPersonByManagerId" resultClass="Person" > select * from PERSON where EMAIL_ID = #value# </select> OR we could make a dynamic Query like the following <select id="getPerson" resultClass="Person" parameterClass="java.util.map"> select * from PERSON <dynamic prepend="where" > <isNotNull parameter="id"> ID = #id# </isNotNull> <isNotNull parameter="managerId"> EMAIL_ID = #emailId# </isNotNull> </dynamic> </select> However there is one pitfall. If the paramete...

Faster webpages with fewer CSS and JS

Its easy, have lesser images, css and js files. I will cover reducing number of images in another post. But If you are like me, You always write js and css in a modular fashion. Grouping functions and classes into smaller files (and Following the DRY rule, Strictly!). But what happens is, when you start writing a page to have these css and js files, you are putting them in muliple link rel=style-sheet or script tags. Your server is being hit by (same) number of HTTP Requests for each page call. At this point, its not the size of files but the number server roundtrips on a page that slows your page down. Yslow shows how many server roundtrips happen for css and js. If you have more than one css call and one js call, You are not using your server well. How do you achieve this? By concatinating them and spitting out the content as one stream. So Lets say I have util.js, blog.js and so.js. If I have a blog template that depends on these three, I would call them in three script tags. Wh...

Java TimeZone ++: mapping Calendar to Oracle Date or TimeStamp

In Oracle Database date and timestamp columns can be read in java using s[g]etTimeStamp() methods for storing date and time information. However, they do not save the TimeZone information! The read and write operations are done on java.sql.TimeStamp which is a subclass of java.util.Date. When reading and writing to database, they just write out the *face value* (read my previous post on dates for explaination) in Java VM's default TimeZone the code is running on. And read the value too 'TO' the java VM's Default TimeZone. So assuming you WriteDate program on a VM in EST and ReadDate program on another VM in PST (or just change system timezone) - You are getting a different *value* in the date Object. So, How do you tackle this issue. The best way is to set your application to run on a specified (constant) default timezone. As mentioned in the earlier post this can be achieved by TimeZone.setDefault() . If you are unable to do it (For reasons unknown to me , so far),...

(NOT) yet Another Getting Started with OSGi

Last week I have been learning OSGi. I got my first application running in OSGi. I am not going to write another getting started post with code that will show a hello world. But rather give you an idea how to start getting in the mud. And then may be show you more than a direction :) I used Equinox 3.4 ( OSGi R4 ) implementation. I prefered it over Felix. Why - Eqinox is simple, and a single jar download. But I guess that aint a thing to notice. You can get Equinox jar from here . If the link is broken, go here , download the equinox framework, it should be ~0.95MB. Start equinox framework as java -jar equinox.jar -console You would have seen this on a lot of sites. but I did fall on two things flat on my face; missing -console and doing it wrong --console :P As per the spec, (and by now, you know ) a bundle is a jar with some special informtion in MANIFIEST.MF. A Bundle jar need not DO anything. It could just sit installed or be active (by exporting packages and services). So ...

MANIFEST.MF in Plain Java, EJB, OSGi - catching the drift

While reading about OSGi, I realised how the importance of MANIFEST.MF has grown over time. The once only-a-jar-information file has had its ups and downs. When I was in school, *chapter jar* was an "assignment" / "reference". It wasn't worth a class' time spent on it. Today a whole new technology relies on this humble text file. Past: Long long ago, in stone age (yeah I am exaggerating), the MANIFEST.MF was auto-generated while jar-ing. More or less it was not used. Well, yeah if you are developing a java application to be distributed to end-users (the jars you used to double click, which are so replaced by jnlp a.k.a WebStart apps now). This file would give a place to specify the class path Manifest-Version: 1.0 Created-By: 1.4.2 (Sun Microsystems Inc.) Main-Class: com.big.long.package.MyAppMain Class-Path: mod1.jar mod2.jar lib/lib.jar This will be set when running java -jar myapp.jar which other wise would have to be something like java -jar myap...

Mission OSGi

Finally some time for OSGi. With release of Spring dm Server, My interest for OSGi got two-fold. Although I did have some close-encounters with OSGi (aah its just a jar called differently - bundle; and MANIFEST.MF drives it all, o I know OSGi), I haven't read into a lot of it. So now, I am diving into it. To start I picked Neil's blog . He also has a draft of his upcoming book, OSGi in Practice . So It is a good place to start. That gives me to start a new tag (osgi) :)

classpath*: making your Modular Spring Resources

Spring gives multiple options to load XML resources for building contexts. the reference documentation does explain this feature quite well. However, I am taking my shot at explaining the different practical scenarios ( by order of growing modularisation) For Example, A simplest Spring based web Context Loader can be configured with resources like this <context-param> <param-name>contextConfigLocation</param-name> <param-value>applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> You just need to put applicationContext.xml in WEB-INF/ folder of your webapp. However, Typically an application is n-tiered. You can also have multiple files setup and in relative paths. like <param-value> context-files/applicationContext.xml context-files/dao.xml context-files/service.xml </param-value> ...

Using Spring XML Schema based bean definitions

Spring's bean definitions (and configuration files) may go very verbose after your projects goes two or three iterations old. By verbose I mean, too many bean definitions, and complex injection graph. And many a times, these xml files go fat because of common definition types - props, lists, etc. These beans are generally configured using FactoryBean s. To reduce this verbatim Spring provides XML schema based configuration, for various types of FactoryBeans. There are some shortcuts presented in that appendix, I frequently use. I recomend those to others too. Here are some of those. <!-- creates a java.util.Properties instance with values loaded from the supplied location --> <bean id="jdbcConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" value="classpath:com/foo/jdbc-production.properties"/> </bean> Could be written as <!-- creates a java.uti...

Moving on to Ivy.

Maven has its share of success, at the same time it has some shortcomings. Many felt the Idea was right, the implementation was not. There were a few rants too. Project Ivy was started on that note . Today, Everyone is talking about Ivy and How Ivy can fix your woes from maven . So, I began to test drive Ivy. Ivy has got pretty good documentation. And the edge over maven is, that you dont have to adapt your project to it (like maven). You can make ivy adapt to your project. All you need to start is a jar to put in your ant lib folder. The examples list a mighty hard way of starting with ivy - So I modified a little bit to get it easy and install ivy automatically. Also this can be used as a template build.xml file for starting from scratch :) <!-- Use namespace even before bootstrap --> <project xmlns:ivy="antlib:org.apache.ivy.ant" default="build" name="SarathPOC"> <property file="build.properties" /> <path id=...