Sunday, February 1, 2015

Using Spring with Hibernate and c3p0 Connection Pool.

C3P0 is a very nice tool to manage database connections. I had hard time configuring Apache DBCP/2 so tried C3P0 and it was very easy to use.. There are many config options to set and the setting has to be done carefully so that we do not end up choking our database. Lets understand some the config options.

  • testConnectionOnCheckin validates the connection when it is returned to the pool. testConnectionOnCheckOut would ensure active connections before use, would be too expensive to do.
  • idleConnectionTestPeriod sets a limit to how long a connection will stay idle before testing it. Without preferredTestQuery, the default is DatabaseMetaData.getTables() - which is database agnostic, and although a relatively expensive call, is probably fine for a relatively small database. If you're paranoid about performance use a query specific to your database (i.e. preferredTestQuery="SELECT 1")
  • maxIdleTimeExcessConnections will bring back the connectionCount back down to minPoolSize after a spike in activity, the connection is removed from the pool and returned back to db.
  • numHelperThreads it will help c3p0 spaws helper threads to manage the connections and returning them back

My spring configuration goes as -

<bean id="dataSource"      class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass">
        <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="jdbcUrl">
        <value>jdbc:mysql://localhost:3306/api</value>
    </property>
    <property name="user">
        <value>root</value>
    </property>
    <property name="password">
        <value></value>
    </property>
    <property name="idleConnectionTestPeriod">
        <value>300</value>
    </property>
    <property name="maxIdleTimeExcessConnections" value="180"/>
    <property name="maxPoolSize">
        <value>100</value>
    </property>
    <property name="acquireIncrement">
        <value>1</value>
    </property>
    <property name="maxStatements">
        <value>0</value>
    </property>
    <property name="minPoolSize">
        <value>10</value>
    </property>
    <property name="unreturnedConnectionTimeout">
        <value>3600</value>
    </property>
    <property name="preferredTestQuery">
        <value>SELECT 1</value>
    </property>
    <property name="initialPoolSize">
        <value>10</value>
    </property>
    
</bean>




Things to keep in mind - 

  • Please try to check the number of connection the app has hooked up with the database. For MySql try -  SHOW STATUS WHERE variable_name = 'Threads_connected';
  • When using Hibernate try to take care of opening and closing of sessions. If sessions are not properly closed, the connections are not freed and eventually it will choke the database.
  • 
    
    
    
    How to configure c3p0

    Saturday, September 20, 2014

    Jackson JSON parser - Writing Custom Deserializer

    One of major difference between Jackson and GSON is how they handle json parsing and mapping the json to a class object. While doing so Jackson uses strict checking which means the object from which the json was created and the class to which the json has to be mapped should be in strict sync.

    Consider the following example -

    class Foo {
        private String value;
    }
    
    class Bar {
        private String value;
    }
    and

    String json = "{\"value\" : \"whatever\"}";
    new Gson().fromJson(json, Foo.class);
    new Gson().fromJson(json, Bar.class);
    Gson is setup to perform a best effort to deserialize the given JSON into an instance of the given class. It will map as many fields as it finds. If none are found, that's too bad, mapping will not be done.

    Coming to the custom deserializer thing. We may need to write one in few cases as - changing date format of the json string to our custom one. For writing our custom deserializer we need to write a class which extends JsonDeserializer<T>. In this class we implement  the method deserialize(...) which contains our custom conversion code.

    public class CustomJsonDateDeserializer extends JsonDeserializer<Date> {
    @Override
    public Date deserialize(JsonParser jsonParser, DeserializationContext context)
            throws IOException, JsonProcessingException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm a z");
        String date = jsonParser.getText();
        try {
            return format.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
      }
    }

    Now annotate the field in the POJO which has to be deserialized using the custom deserializer.

    @JsonDeserialize(using = CustomJsonDateDeserializer.class) private Date createdAt;

    That's it. Pretty simple thing.

    RestEasy 3.0


    We were using RESTEasy 2.0 in on of our earlier projects. RESTEasy is a JBoss project that provides various frameworks to help you build RESTful Web Services and RESTful Java applications. The 2.0 version was quite simple to use. I decided to use version 3.0 in one of recent projects and the migration was not that tough. A lot of chages have been done in the newer version. I am going to mention the ones which I found out.

    RESTEasy Client 

    In version 2.0

            String url = "http://resetservice.com/api/testService";
            ClientRequest request = new ClientRequest(url);
            request.body("application/json", json);
            response = request.post();
            int status = response.getStatus();
            if (status == 200) {
                String data = (String) response.getEntity(String.class);
                Boolean value = new Gson().fromJson(data, Boolean.class);
                return value;
            }
            return false;
    
    

    The same thing  in 3.0 now changes to -

         ResteasyClient client = new ResteasyClientBuilder().build();
         String result = getResponseString("http://resetservice.com/api/testService");
         Boolean returnVal = clientUtil.getObjectFromJson(Boolean.class, result);
         return returnVal; 
    
    
    
    
    Authentication

    I also had to use some in-house authentication technic.  I used it in very basic way. I checked for a particular request header (authorizationString) for authenticating the request. If that header is not present the request gets aborted. I used a very basic Below is the code snippet for the same.

    @Provider
    @ServerInterceptor
    public class AuthorizationRequestFilter implements ContainerRequestFilter {
    
        @Override
        public void filter(ContainerRequestContext requestContext)
                throws IOException {
            final SecurityContext securityContext =requestContext.getSecurityContext();
            String header = requestContext.getHeaderString("authorizationString");
            try {
                if (securityContext == null || header == null || header == "" ||
                    (header != null && !header.equals(
                    ConfigReader.getInstance().getAuthToken()))) {
                requestContext.abortWith(
                        Response.status(Response.Status.UNAUTHORIZED).entity("User
                            cannot access the resource.").build());
            }
            } catch (ConfigurationException e) {
                e.printStackTrace();
            }
        }
    }



    Friday, September 19, 2014

    Amazon RDS with MySql - How to create an instance!

    This post will guide through how to use Amazon RDS, the steps of creating an instance and other related things.

    Before going further one can take a look on few links to get a hang of what RDS is all about and how does it perform against other available solutions.




    Lets get started.

    1. Launch DB instance - AWS Services are relatively cheaper in USA region, so you can consider about choosing the region. Go to Amazon AWS console and select RDS from there. You will land up on the below page. Click "launch a DB Instance".


    2. Select mysql engine.



    3. Production- Choose Multi-AZ Deployment.



    4. Specify DB Details - On the basis of your requirements you can choose DB Instance Class. In my case I used db.t2.medium which was of 5GB capacity. Specify user and password for the db from here.



    5. Configure Advanced Settings - You can define your custom parameter group so you can simply choose default parameter group here. If you have a pre-defined security group, you should choose that one here. 


    6. DB Instance Created- The database has now been created. It may take couple of minutes for the db to set up completely.


    7. The newly created DB instance will start reflecting in the dashboard.






    Changing the default configuration variables of MySql

    Now after the db instance has been created we need to change a bit of configurations and monitor the db.

    I struggled a bit when I had to change the default configuration values. To change the config values (equivalent to changing values in my.cnf file) we have these following steps.

    1.  Go to RDS dashboard and select the database which you have created. Choose "Parameter Group" from the left pane.





    2. Click on "Create DB Parameter Group" to create a custom Parameter Group. Choose some suitable name and description.


    3. Now choose "Edit" to change the config. Some important variables as - max_connections, query_cache_size etc can be changed from here. The changes will be applied and reflected.




    Monitoring


    One of the coolest features of RDS is its monitoring. You can check out for n different metrics. - CPU Utilizations, Number of connections etc.



    Simple, isn't it? Feel free to comment.

    Thursday, October 25, 2012

    A Bong Land Away From Bengal

    Chittaranjan Park (Bengali: চিত্তরঞ্জন পার্ক), aka C.R. Park is a neighborhood in South Delhi, and home to a large Bengali community in Delhi. That's a very known fact. People say, if you want to get a feeling of how Durga Puja is celebrated in West Bengal, then go to this place. I visited this place on the last day of Puja and witnessed the show and it was totally worth it. 

    The Bengalis go little insane during Puja period. They have their own way of celebrating it. Seems like there is a sudden induction of too much happiness in these people. And thanks to whatever it is, the place turns into an electric one during the Puja. The streets were decorated with light bulbs - not too bright.  Too many people walking the streets. Kids shouting, girls giggling and ladies blabbering. Too much of chit-chat and I could not hear myself at times. Initially I was little disturbed by the noise, but after I walked enough along with the crowd I start enjoying it. And then there were things around which kept me engaged. The sound of drums, flock of girls in ethnic dresses dancing on a stage nearby the Pandal, a beautiful woman in red embroidered sari singing some classic Bangla song, young girls chatting shyly with boys near the food stalls, smell of fish in and around the Pandal. Everything was oozing out of itself some kind of Bengali class. 

    Though I am not a great fan of Bengal/i, but one thing I must mention that I like is - they carry their culture wherever they go. You must be having some Bengali family living near by and they having your mind blown off with some great foods or at least some great variety of sweets. They make you feel the impact of rich culture own and they belong to. A single Bengali family can form community of its own and then slowly engulfing the people near by. A small place secluded from the typical charm which Delhi carries was what the place appeared. The streets reminded me of  a typical Kolkata streets minus the crowd part. It was indeed a nice experience. 

    Sunday, September 23, 2012

    It Was Dreddfully Fantastic



    Cast: Karl Urban, Olivia Thirlby, Lena Headey, Wood Harris
    Director: Pete Travis


    For a long time we all have been waiting for a perfect action movie where there is no Super-hero but a bad-ass toughie. Lessons, protagonism and philosophy ruin the essence of action. A perfect action flick is the one where we get some gory stuffs, gunshots, some dark humor, rushing sequences and  a music to elevate all these up to a different level. With Dredd you are guaranteed to get everything. A perfect violence crime thriller with every element presented in pristine form. Right from the start, the movie assures you that you will be fed a great dose of insanity and in the end you have with you a head swirling on the tune of it.

    The movie, set in future,  revolves around busting of a ghetto gang homed in a multi-storeyed led by an unattractive blood crazed lady called Ma-Ma. This gang deals in production and selling of drugs and their main product- Slo Mo - which slows the perception of time of the addicts. The slow motioned effects of this drug has been so wonderfully portrayed. Everything stops and you feel yourself to be the subject. Judge Dredd, character led by Karl Urban, is a ruthless executioner who believes in law and exterminates every one who goes against it. He teams up with a rookie who is a mind-reader too and very strategically uproots the gang.

    The plot may not be the greatest, but it does not have to be. What matters the most is the presentation. And for that the movie wins a standing ovation. Cinematography, I must mention, is breathtaking - as good as it should be. The futuristic city - chaotic, gloomy and plagued by social malaise - speaks off its story. The slow motion acts are one of many great things about the movie. Who needs a description on acting now? Karl Urban is such a perfect fit for the lead role. With laconic dialogues in husky voice, the helmeted actor will surely win your heart. The character(of a rookie) played by Olivia Thirlby is very powerful. She is on a par with Dredd - witty and equally intelligent. The music is just divine. It keeps you on your toes every time. The thrilling sequences coupled with great background music will make your heart slip at times.

    Dredd is what we have been looking for so long. It has been crafted very nicely. A perfect juxtaposition of brutality and violence! Leave everything behind and behold to those bloody sequences. It would very foolish mentioning what 3D effects have done to the movie. For those who enjoyed Kill Bill and District 13, here is Dredd satisfying the hunger of both.

    Monday, September 17, 2012

    Barfi - The New Face Of Indian Cinema



    “Man's mind, once stretched by a new idea, never regains its original dimensions.” 

    What will you remember the year 2012 for? I shall ever remember it for some of the greatest movies our quality deprived Indian Cinema produced. Seems like all the film-makers have suddenly realized that  all good work requires nothing but self-revelation. And they are working on it - producing results which the human race would have never expected of. They have suddenly realized that experience can let you experiment and that if it turns out to be successful, can lead you to a perfect evolution. Time's evolving. Yes the process has been little slow, but look what its fetching you. For those who used to believe in the power of cinema must be feeling very relieved, so am I.

    It would be very derogatory for me to say anything about Barfi. If I start talking about it, may be I run out of words, because doing so is simply a gargantuan task.  Someone once said - "There are two types of films: those that employ the resources of the theater (actors, direction, etc...) and use the camera in order to reproduce; those that employ the resources of cinematography and use the camera to create". I say there exists a third category which involves the both in an appropriate proportion and produces something miraculous, something beyond imagination. Barfi is a combined effort of an immaculate direction, a great script and some never seen before acting. Gone are those days when it used to take a cheapo to cry in a directionless movie and get applauds from the lost souls. You now need to have a proper substance and then only you can leave an indelible imprint on the million minds. 

    When it comes to portray disability on the celluloid, it takes something more than humanitarian efforts for the entire crew.  The characters have to speak with their eyes, their movements must speak. The direction has to be impeccable. Not a single emotion should escape the mind of the audience - immersive -  so should be the power of expression. An autistic person's role has to be the most difficult and needs a sorcerer's touch. I am simply reminded of Dustin Hoffman's role in Rain Man and Priyanka Chopra played it no less exceptional - one of the most touching performances I have ever seen. Ranbir Kapoor has properly fit himself in any kind of role he has ever been assigned. He is mute yet so expressive, funny yet so touching. And the love-story of these two disables has been so wonderfully and subtly knitted that once you are entangled you have no escape. 

    Burfi opens to you doors to an ethereal world created through a poetic imagination. With all these excellence the team might have fetched themselves numerous awards already and yet again set examples for all the aspiring actors and directors.  All it takes for them to align to the same trend. Its the time we can see cinema riding the path of Resurrection. We were never out of talent but the ideas were just lacking. Movies like Kahaani, Paan Singh Tomar, Barfi and others have shown to the world how much potential we carry. With a sudden out-pour of fresh new ideas for last few months, the faith in cinema has certainly been restored. We can expect more and more stunners in coming times to blow and boggle our minds.