I am working on a implementation which requires using websockets with failover to comet. Web socket is an upgrade protocol to HTTP, and once established the connection remains open between server and client. Typical HTPP load balancers will not work – so what is the solution.
I did some research on loadbalancer with specific support for Websockets –it turns out that HAPROX is the most reliable and widely used open source option. Other option is NgNix, having community version as well as paid model –but community version lacks active support.
Apache has also released it’s module for websockets, but reviews are not as good as HAPROXY. A good comparison and discussion around these three is available in this post.
The “Greatest Limitation” of HAPROXY –it does not have a build for windows.
It can be worked out for testing with cygwin on windows -but for any production deployment, it has to be a linux machine.
It could be weird but when you code in hurry to get over ASAP, it invites even more delay and trouble -I spend almost 2 hrs trying to access static files placed in a folder inside WEB-INF folder, forgetting that it will never be accessible from a browser because that’s why there exist a WEB-INF folder -to restrict public access!
So my intent was good – I had a JSP which has embedded SWF file. Everything was in root foler parellal to WEB-INF. Then I though lets restrict access to SWF using direct URL (SWF have some login code as well) – So I created a folder view “inside” WEB-INF and moved all my files there. hmm..
- After execution of business logic, I forwarded the request to JSP .
- JSP was rendered but SWF was not loaded.
- I tried redirect by putting the folders outside WEB-INF (For redirect it has to be out side WEB-INF!) and it worked!!!
- Oh my –I concluded, it’s issue with the forward, completely ignoring the fact that it’s about static file location
- After 2-3 hrs, yeah, 2-3 hrs, I used Firebug -I do not know why I did not used at the beginning, may be I was trying to be over smart and highly confident about my analysis of the cause!
- Firebug reveled that all files need to be at a place accessible by browser -which is outside WEB-INF
More than any technical learning – do not be overconfident about your analysis 😉
Recently I got rid of XML files in Spring app used for configuration by using annotated classes. A basic issue -how to “lookup” these instance if required, took me some time. The good old way with XML
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("/META-INF/application-context-root.xml");
It’s still simple, use a different class with configuration class as argument
ApplicationContext context = new AnnotationConfigApplicationContext(RootConfig.class); sessionManager = (SessionManager) context.getBean("sessionManager");
Recently while doing a code review I came across a code which was using date functions in Action script (Adobe client side code) to print payment receipt date.
For persisting the payment date, it was being read from server -But for printing on receipt it was read from client machine.
I got an explanation that why to send a data which can be read same way on client (Referring to equivalent of getCurrentDate) -missing the point that we should never rely of stuff which can be manipulated outside out control.
MySql is configured to drop any connection which has been Idle for more than 8 hours. What is the implication of this? After you return to your deployed app after a gap of 8 hours (If default SQL parameters have not been changed), you will be greeted with an exception.
How to solve this issue?
- Increase the wait_time parameter -Not a good Idea, it might unnecessarily hold on to the resources and not might be a sure shot way. Apart from that, being dependent on an “external” configuration for failover is not a very good idea -what if the server itself crashes, what if this configuration is missed in one of the instnaces, and many such issues will pop up against this approach.
- Use the parameter autoReconnect=true with JDBC URL -My SQl itself does not recommend this, have a look at link and people have reported that this does not work as well, refer link.
- Custom handling -have your code identify that connection has been lost and then recover it and try to reconnect, but then it would be a lot of fail over mechanism in code.
- The best way I found was to configure pooling mechanism as c3p0. See this post how to configure c3p0 in JPA for hibernate, it’s simple, easy and reliable.
So how do you test that issue is solved?
- Change wait_timeout in MySql to just 2 minutes, this is how it can be done from MySql workbench admin console
- Keep value of idleTestPeriod less than wait_timeout -A quick recap what idleTestPeriod signifies
- Login after wait_timeout has passed -it should not throw a exception
idleTestPeriod: default value=0; If this is a number greater than 0, c3p0 will test all idle, pooled but unchecked-out connections, every this number of seconds
I added c3p0 for pooling in JPA, but encountered an exception
Unable to load class [org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider]
My configuration looked like
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" /> <property name="hibernate.c3p0.max_size" value="10" /> <property name="hibernate.c3p0.min_size" value="0" /> <property name="hibernate.c3p0.acquire_increment" value="5" /> <property name="hibernate.c3p0.idle_test_period" value="60" /> <property name="hibernate.c3p0.max_statements" value="0" /> <property name="hibernate.c3p0.timeout" value="100" />
Details about these properties and other defined at link
Looking at log trace it’s not difficult to figure out that jar is not correct, so first change, upgrade to latest c3p0 artifact.
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>3.6.10.Final</version> </dependency>
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>4.1.9.Final</version> </dependency>
After changing this, it worked but printed an Warning
WARN - HHH000208: org.hibernate.connection.C3P0ConnectionProvider has been deprecated in favor of org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider; that provider will be used instead.
This indicates that provider_class should be changed to remove the depricated class, so it should be
<property name="hibernate.connection.provider_class" value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" />
This cleanly integrated the c3p0 implementation.
I am using JPA with hibernate underneath. Using the property
<property name="hibernate.show_sql" value="true"/>
Shows just the SQL and not the values of parameters passed -it displays ? marks for query parameter as incomeexpe0_.transactiondate)>=?
Want to know what was the exact parameters passed -This is real helpful for debugging.
This can be achieved using hibernate logging -Add following two lines in your log4J.properties file
If this is used “log4j.logger.org.hibernate.SQL=TRACE” -No need to use property hibernate.show_sql, it wil take care of dumping queries.
The secondstatement, “log4j.logger.org.hibernate.type”, logs the JDBC parameters passed to a query
TRACE [main] (BasicBinder.java:83) - binding parameter  as [TIMESTAMP] - Thu Jan 03 01:00:18 IST 2013 TRACE [main] (BasicBinder.java:83) - binding parameter  as [TIMESTAMP] - Thu Jan 03 23:00:19 IST 2013
For even more advanced analysis and precise JDBC formatted queries (Non in an ORM form, but REAL sql), jdbc proxy driver like P6Spy can be used.
With the arrival of ORM most of operations related to DB, like managing connections, transaction management have been abstracted to a certain extent and this has led to many people believe that we can directly call the entitymanager from service layer. I agree that we hardly change persistence layer ( atleast in services, for products its very much can be a valid scenario), But then can we overlook following advantages of DAO layer (All related to DRY)?
- Exception handling at a centralized location for all DAO issues -most of time service layer just pass on the failure to client and do not take any action. DAO can logg the exception, for example hibernate exception, log the query -these all can help dig the issue, and throw a runtime exception which will keep service layer code cleaner
- What if same query is being called from multiple location -even inside same service, for example “get list of all students”. A change in in this query, lets say an additional criteria, will have to be tracked across all occurence . If there is DAO, it will be delegated there and changes wil be at single place
- A well designed DAO layer can be used across projects -for example recently I wrote a DAO for JCR queries, and most of its part as create node, create heirarchical node, delete node etc were exposed in a manner that the lib was used across project
- We might not change the DB, but may be ORM itself need to be changed -for example one of client code which needed a deployment on Google apps had to change from hibernate to JPA (Google apps support JPA but not hibernate) -having a separate DAO made life simpler
On flip side, this does introduce more layers and extra level of delegation, but I feel it’s worth it.
A flex popup -typically of type title window, is by default 50% of the size of its parent and with hel of PopUPManager, it can be centered when loaded.
What if requirement is to have a fuller size popup window -size of it’s parent container?
- One way is to use fixed height and width
- other way is to resize it wrt it’s parent as
Not to say that first approach of using fixed height and width must be avoided as it will break on different screen sizes.
Approach two seemed more elegant to me .