Monday, November 7, 2011

Threading Questions

What is the difference between volatile and synchronization?
http://www.developertutorials.com/tutorials/java/use-synchronized-volatile-when-accessing-shared-variables-050525-1445/

Disadvantages of using Synchronization
Explain the API of java.io.Concurrency in Java 1.5

Friday, April 29, 2011

ITunes for Linux

Such a thing does not exist !! :)

Install wine for linux.

$ sudo apt-get install ubuntu-restricted-extras
$ chmod +x iTunesSetup.exe

OR

try Banshee for Ubuntu

Friday, August 27, 2010

Association, Aggregation, Composition


In normal terms, they both refer to member object but the survival or existence of the member object without the containing class or object or after the lifetime of the containing class or object makes the difference. Aggregation is also known as a 'has a' relationship because the containing object has a member object and the member object can survive or exist without the enclosing or containing class or can have a meaning after the lifetime of the enclosing object also.
Example ('has a'): Room has a table and the table can exist without the room. The table can have meaning without the room also.



Composition is also known as a 'is a part of' or 'is a' relationship because the member object is a part of the containing class and the member object cannot survive or exist outside the enclosing or containing class or doesnt have a meaning after the lifetime of the enclosing object. Composition is used over inheritance when different roles are to be played by a single entity. The 'is a' relation comes in this case.
Example 1 ('is a part of'): Computer Science Department is a part of the College. The Computer Science Department cannot exist without the college and the department has no meaning after the lifetime of the college.
Example 2 ('is a'): A Person is a supervisor. A Person is a husband. Supervisor and husband are the roles played by a single person. The husband role without the person has no meaning or cannot exist and similarly for the supervisor also.






From the above figure, Battery and Smart Phone explain Aggregation while the other relations explain Composition. The Smart Phone has a Battery. The Battery can exist without the Smart Phone and it has a meaning without the Smart Phone. On the other hand, the IMEI Number is a part of the Smart Phone and the existence of the IMEI number completely depends on the existence of the Smart Phone. The IMEI Number has no meaning without the Smart Phone. While the relation between IMEI Number and the Smart Phone explains the 'is a part of' type of composition, the other relation explains the 'is a' type of composition. The Smart Phone, in this case, plays roles at different times. The Smart Phone is a Camera. The Smart Phone is a Web Browser. The Smart Phone is a Music Player. The Music Player role of the Smart Phone cannot exist when the Smart Phone itself is not available.



Thus, Aggregation and Composition are almost the same conceptually but differ in implementation because, the contained or member object should be deleted in case of composition while the member object should not be deleted in case of aggregation.






Association - it's a structural relationship between two classifiers - classes or use cases. Cyclic association is also possible when the same entity plays two different roles and contains an association describing the relationship between the two roles.

In UML notation an association is depicted by a solid lineFor example: suppose we have two entities Employee (EmpNo, Name, EmailId, Salary, DeptNo, Address) andDepartment (DeptNo, DeptName). Now these two entities are having an association. An association can have any cardinality at any of the two ends depening on the particular relationship.

Aggregation - this is special type of association used for modelling 'Possession' (not Ownership) with the restriction that it's NOT cyclic. That means, an entity can't have an aggregation relationship with itself. This is the reason why an aggregation relationship doesn't form a Graph, instead it forms a Tree.

Aggegation is normally understood as a "has-a" relationship. Here both the entities continue to have their own independent existence. In UML notation, an aggregation isdepicted by an unfilled diamond and a solid line. For example: Consider two entitiesEmployee (EmpNo, Name, EmailId, Salary, DeptNo, Address) and Address (FlatNo, PlotNo, StreetName, Area, PinCode). As we might have noticed that every Employee has an Address, but the lifetime of the an Address instance is not governed by any Employee instance. It may continue to exist even after the reclaimation of the Employee instance. Right?

Composition - this is also a special type of association used for modelling 'Ownership'. This is very similar to Aggregation with the only difference that it depicts a Whole-part relationship and the 'part' entity doesn't have its own independent existence. The existence of entity depicting 'whole' giverns the lifetime of the 'part' entity as well. For example: Consider two entities Employee (EmpNo, Name, EmailId, Salary, DeptNo, Address) and EmailId (ID, domain). Now an independent instance of EmailId entity doesn't really make sense unless it's associated with an Employee instance.

Composition is normally understood as a "contains-a" relations. Similar to Aggregation, a Composition also forms a Tree only and not a Graph as it can't be cyclic. In UML notation, a Compsoition is depicted by a filled diamond and a solid line.

Aggregation vs Composition
  • Aggregation represents a "has-a" relationship whereas Composition represents a "contains-a" OR "whole-part" relationship.
  • In case of Aggregation, both the entities will continue to have their independent existence whereas in case of Composition the lifetime of the entity representing 'part' of the "whole-part" is governed by the entity representing 'whole'.
  • Aggregation represents a loose form of relationship as compared to that represented by Composition which depicts a stronger relationship.
  • In UML notation, an Aggregation relationship is depicted by an unfilled diamond and a solid line whereas a Composition relationship is depicted by a filled diamond and a solid line.
  • A 'part' of a Composition relationship can have only one 'whole' at a time i.e., a multiplicity of 1 whereas an Aggregation relationship can have any multiplicity 0..* at the aggregate end.

Tuesday, August 17, 2010

How-to-create-datasource-in-tomcat

I am using tomcat-5.5, Mysql 5.1.27, Spring 3.0

Keep all the Spring libraries in D:\apache-tomcat\webapps\SpringWeb\WEB-INF\lib
Copy the JDBC Connector library in D:\apache-tomcat\common\lib

I first created a datasource on tomcat.
Modify the D:\apache-tomcat\conf\context.xml file to include this tag.

< resource auth="Container"
driverclassname="com.mysql.jdbc.Driver"
maxactive="100" maxidle="30" maxwait="10000"
name="jdbc/HWMD_DS"
password="pratik"
type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/HOW_WAS_MY_DAY"
username="pratik"
/>

I am using plain JDBC with Spring to connect to the database.

This is how the Spring configuration file will look like.

< beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd">

< bean class="org.springframework.jndi.JndiObjectFactoryBean" id="dataSource" scope="singleton">
< property name="jndiName" value="java:/comp/env/jdbc/HWMD_DS">
< property name="resourceRef" value="false">

< bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
< property name="dataSource" ref="dataSource">

< bean class="dave.spring.dao.JdbcRantDao" id="rantDao">
< property name="jdbcTemplate" ref="jdbcTemplate">

This Spring configuration should be in application classpath. ie
D:\apache-tomcat\webapps\SpringWeb\WEB-INF\classes\myapp-dao.xml

Configure Tomcat Datasource in Hibernate

< ?xml version="1.0">
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
< hibernate-configuration>
  < session-factory name="java:comp/env/hibernate/SessionFactory">
    < property name="connection.datasource">jdbc/myDataSource</property>
    <property name="dialect">
        org.hibernate.dialect.MySQLDialect
    </property>
    <mapping resource="com/manning/hq/ch03/Event.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

iBatis Vs Hibernate


Which is better ?IBatis or Hibernate? 

IBatis maps the Java Objects to the SQL Query’s results. Hibernate, maps Java Objects to DB tables (Object-Relational Mapping).
Hibernate automatically generates all the SQL for you. In IBatis you can directly write queried in SQL, thus if you are a good SQL programmer, you would find learning Ibatis very easy. Thus Ibatis allows you to do anything what could be done through SQL. Thus you can virtually know what the affect of a query would be.

  1. Hibernate is excellent for CRUD queries (Create, Read, Update, Delete) queries, and in applications where the object model is already ready.
  2. IBatis is much simpler to learn compared to Hibernate .
  3. On the other hand, Since you need to write all the queries by yourself, It has a much steeper development time and maintainability is comparatively difficult in Ibatis.
  4. Also if there is a change in object – model, one needs to go through all the queries and correct them, which isn’t much of a problem in hibernate.
  5. Hibernate does not support stored procedures. Ibatis does.
  6. IBatis is faster in terms of query processing time
  7. IBatis is better for batch inserts queries.
  8. IBatis is simpler.
  9. IBatis supports Dynamic Query which Hibernates doesn’t.
  10. Hibernate is close to Object Oriented development. Ibatis is close to relational DB.