Friday, February 14, 2014

Accessing Persistent Objects

In the previous article, I have discussed how to create session instance. This article demonstrates how session object can be used to access persistence objects, insert, update or remove objects.
[Assumption: we have session object, and we have to close transaction and session after we complete operation]

1) Insert, Update or Delete persistence object

  session.saveorUpdate(object);
  session.delete(object);

2) Retrieve a collection of data
   i) If you want all persistent objects
       //using hibernate query
       List list=session.createQuery(HQL).list();
       //using sql 
       List list=session.createSQLquery(SQL).list();//gets raw
       List list=session.createSQLquery(SQL).addEntity(className).list();//entity objects

   ii) If you want selected object only (similar to WHERE clause in SQL)
 
     String HQL="SELECT * FROM ENTITY entity WHERE entity.fieldName=:fieldname";
     session.createQuery(HQL).setString(fieldname, value).list();
 
     For SQL, we create the sql query to get the list.      

    Criteria is more elegant for selection.
   
      Criteria criteria = session.createCriteria(className);
       criteria.addOrder(Order.desc(fieldName));
       criteria.add(Restrictions.ge(fieldName, value));
       criteria.add(Restrictions.le(fieldName, value));
      criteria.setMaxResults(limit);
      
      // Also define union and intersection
       Disjunction disjunction = Restrictions.disjunction();
       disjunction.add(Restrictions.eq(fieldName, "#BLABLA#"));
       criteria.add(disjunction);      
       Conjunction conjunction=Restrictions.conjunciton();
       conjunction.add(Restrictions.eq(fieldName, "#BLABLA#"));
       criteria.add(conjunction);    

        criteria.setFetchMode("whatever", FetchMode.SELECT);

     List objects= criteria.list(); 

3) Retrieve specific object

We have to note here that all persistence objects have their own id. So,  with the id, we can get object

 Object object=session.get(className, id);

Hibernate Basics

Recently, I faced a lot of problem implementing persistence of objects with the database using Hibenate persistence API. If we know exactly, its very easy and straight task implement persistence on the objects with database, otherwise, we might get very peculiar problems where it takes a lot of time to get out of it.

In this article, I will present my experience creating persistence objects using PostgreSql database. I will start from beginning so that you can directly use the source code in your implementation.

1) Get Data Source
Use the following method to get datasource. We use this later when creating properites.
private DataSource getDataSource(String host, String database, String userName, String password) {
PoolProperties poolProps = new PoolProperties();
poolProps.setUrl("jdbc:postgresql://" + host + "/" + database);
poolProps.setDriverClassName("org.postgresql.Driver");
poolProps.setUsername(userName);
poolProps.setPassword(password);

poolProps.setJmxEnabled(true);
poolProps.setTestWhileIdle(false);
poolProps.setTestOnBorrow(true);
poolProps.setValidationQuery("SELECT 1");
poolProps.setTestOnReturn(false);
poolProps.setValidationInterval(30000);
poolProps.setTimeBetweenEvictionRunsMillis(30000);

poolProps.setMaxActive(75);
poolProps.setMaxIdle(25);
poolProps.setInitialSize(3);
poolProps.setMaxWait(10000);
poolProps.setRemoveAbandonedTimeout(60);
poolProps.setMinEvictableIdleTimeMillis(30000);
poolProps.setMinIdle(10);

poolProps.setLogAbandoned(true);
poolProps.setRemoveAbandoned(true);

poolProps.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;" + "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");

DataSource dataSource = new DataSource();
dataSource.setPoolProperties(poolProps);

return dataSource;
}


2) Create properties object

Properties props = new Properties();
props.put("hibernate.connection.datasource", getDataSource(hostname, database, username, password));
props.put("hibernate.cglib.use_reflection_optimizer", true);
props.put("hibernate.show_sql", false);
props.put("hibernate.hbm2ddl.auto", "update");
props.put("transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory");
props.put("hibernate.jdbc.batch_size", batchSize);
props.put("hibernate.default_batch_fetch_size", fetchSize);
props.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");


3) Create Configuration

Configuration config = new Configuration();
config.setProperties(props);
//class registration
config.addAnnotatedClass(className);

If we have to register all classes in the specified package, then we have to get all classes inside the package.

4) Create session factory

ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(props).buildServiceRegistry();
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);

5) We have to get session for persistent objects, so we get this by:

Session session = sessionFactory.openSession();
session.setFlushMode(FlushMode.COMMIT);

6) While executing query

Session session0sessionFactory.openSession();
Transaction trans=session.beginTransaction();
session.delete(object);
trans.commit();
session.close();


Creating persistence objects will remove the bother of thinking the database structure and tables. We just care about objects, and the back end database operations are carried out by hibernate API itself.


Automating Jobs in Linux System (Crontab)

We install applications and services in the Ubuntu system and they go on running all the time. So, it is not guaranteed that they run perfectly. Even, some services generate huge log files, or some services go on consuming more and more memory, finally, making the system crash with memory leak. So, to minimize those problems, we implement the auto-restart in the system.

Since, we have different system configuration (such as services, hardware and memory), the way we define auto-restart is different for different system, so we don't make it built-in image characteristics.

The settings of auto-restarting of server is quite easy task if we are familiar with Linux's crontab application.

Edit

crontab

We can define the automated tasks using crontab command from the terminal as below:
crontab -e
Then we have to select the preferred editor, after that we go into the editor where we define the tasks to run at specified time. We have to add a line for each task in the following format:
minute hour day_of_month month day_of_week command
Note: every users have different crontab file generated in /var/spool/cron/crontabs with the name of the user.
So, that if enough for theory. Now, if we add the following line
20 01 * * 1 sudo reboot
Then it restarts the system every Sunday at 01:20 am.(The user should have super user privilage to restart the system.)
We can change the values according to our requirements.

Edit

/etc/crontab

We can define user wise cron tasks by editing /etc/crontab file. The format is same as above, only we have to provide the user name can execute the defined task, for example:
20 01 * * 1 frietec sudo reboot
So, the user frietec reboots the system every Sunday at 01:20 am.

[Note: the command provided should be executable by the provided user, otherwise, nothing happens.]

If you look into the file /etc/crontab file, you will notice there is already some automated tasks in the folders /etc/cron.hourly /etc/cron.daily /etc/cron.weekly /etc/cron.monthly defined in the file.  So, an alternative will be keep the task(script) in the specified folder to execute at the define time in /etc/crontab file.

clear log files

Log files in linux system are located under /var/log directory. And the long time running system, in case there is some problem, will grow log files size. So, to prevent the system from crashing, we have to implement the mechanism to clear log files.
A simple method to clear log files is to place following line in the crontab job:
0 11 * * 1 find /var/log -type f -delete
This line will find all log files inside /var/log directory and deletes.
The above command can be executed by root user only, so we keep this in root crontab.


Wednesday, August 28, 2013

Remote Debugging in Java and Eclipse

Why Remote Debugging?

Debugging is very important to find out problems in the software system. It is good idea to find all the bugs and glitches in the system in development period, but no one can guarantee that a system is "bug-free". Nowadays, we have advanced Integrated Development Environments(IDE's) which makes development and debugging very easy. So, if we find out the problem of the software in the development phase, IDE make it easy to find out. But what if we find out the problem in the software system that is deployed to the client's computer where we can't(not allowed to) install our development environment. It sometimes is impossible to install IDE to a system with low resources(such as low memory or processor power etc.).

Remote debugging covers the problems mentioned above. We can remotely debug the software system installed in a computer anywhere in the network. Remote debugging is quite useful to find out system specific bugs. There are many real examples of bugs which do not exist in the development system, however, they exist in the client's system where we deploy our software.

How Remote Debugging?

Well, we will discuss remote debugging in Java (It is possible in other programming language also) with Eclipse as IDE (I guess it is the most popular IDE). To make the process simple, I have divided whole process into steps. The whole process can be divided into two parts:


Java Configuration(Remote)

For class file
To enable remote debugging for a software as a class file ( e.g. HelloWorld.class ), we use the following command to execute.
   java -Xdebug -Xrunjdwp:transport=dt_socket,address=4444,server=y,suspend=y HelloWorld
  
For JAR file
To enable remote debugging for a software as a JAR file ( e.g. HelloWorld.jar ), we use the following command:
   java -Xdebug -Xrunjdwp:transport=dt_socket,address=4444,server=y,suspend=y -jar HelloWorld.jar
  
For Tomcat Server
If the application is running under tomcat server, we have to modify catalina.sh file in $CATALINA_BASE directory. 
Open the file, and you will see CATALINA_OPTS, just un-comment it and replace with this:
   CATALINA_OPTS="-Dcom.sun.management.jmxremote=true 
          -Dcom.sun.management.jmxremote.port=4444 
          -Dcom.sun.management.jmxremote.authenticate=false 
          -Dcom.sun.management.jmxremote.ssl=false"  
  
And you have to stop the tomcat server if it is already started:
   service tomcat stop
  

Go to bin directory under $CATALINA_BASE (e.g. /opt/tomcat/bin) and execute the following command(
  catalina.sh jpda start
  

Eclipse Configuration(Local)
Eclipse configuration is quite straight-forward.

1. Select the project you want to remote debug.

2. Go to "Debug Configurations..." under Run menu.

3. Create a new Remote Java Application.

4. Provide:
Connection Type: Standard(Socket Attach). 
Host: Remote hostname or IP ( e.g. 192.168.1.83 )
Port :4444
Let the option "Allow termination of remote VM" as disselected. That's it.

References: 
1. http://tomcat.apache.org/tomcat-7.0-doc/monitoring.html#Enabling_JMX_Remote
2. http://javarevisited.blogspot.de/2011/02/how-to-setup-remote-debugging-in.html

Thursday, August 8, 2013

Pending Posts

>> Run Job Periodically in a specified time
>> How Java Table works?
>> Simple XML Serialisation(lightweight XML processing tool appropriate for android)
ULR:http://simple.sourceforge.net/home.php

Monday, April 8, 2013

How to create DEBIAN(.deb) installer

Sometimes we feel frustrated when we have to manage large number of files which we need to deploy some application. The files might be scripts, some packages and source codes also. So, to make the process simple, we just create a Debian file installer which packages everything we need. The most interesting thing is we can define the directory structure for the application, like if we need some folder after installation, we can define folder inside the debian installer. We also have post-installation, pre-remove and post remove script files to make the task more simple.

So, what do we need to create a Debian installer?
I have found two important explanations on how we can make it possible. Lets have a look at
http://www.ibm.com/developerworks/linux/library/l-debpkg/index.html

The following also helped me(it seems to be quite old article though)
http://tldp.org/HOWTO/html_single/Debian-Binary-Package-Building-HOWTO/

We need the following two packages

build-essentials (it is already intalled in most of the linux systems)
dpkg-dev

We use the following commands to create the package:

dpkg -b directory package.deb

There is a good tool called lentian, we can install it to find out the problem. It is a like required tool while creating the debian installer.

The above mentioned links are quite informative.

Thursday, April 4, 2013

Ubuntu Tips

1) How install and set Mate-Desktop as remote desktop
  sudo apt install mate-environment
  sudo apt install xrdp
  [sudo apt install vino]
  echo mate-session> ~/.xsession

   Restart the system

The most usable and popular Ubuntu tools:
Dia: Drawing structured diagrams
LibreOffice4: For office purpose
Inkscape: Vector Drawing tool
OpenSSH: SSH server
Chromium-browser: Browsing tool
BeyondComparer/Meld: File or Folder comparison
VirtualBox: For virtually running Operating systems
CloneZilla: Creating clone image of hard disk or partition
Gparted: For hard disk partition
WPS: Replaces office
rdesktop: Remote desktop



Cleaning commands before creating image:

1) apt-get autoremove
2) apt-get autoclean
3) apt-get clean
4) history -c
5) clean browser histories, caches 




How to make USB bootable for windows OS? 
  • Format your USB as FAT32 in GParted
  • Open UNetbootin and get it as far as the stage where it brings up the USB partition to install to e.g. /dev/sdb1 - Don't install the ISO, though
  • Leaving UNetbootin open as is, reopen GParted
  • Format the USB in GParted as NTFS
  • If GParted doesn't automatically add the "boot" flag, add it yourself
  • Now, go back to UNetbootin, which you've left open in the meantime, and click OK
Doing so, UNetbootin will think you are using a FAT32 partition and will let you use NTFS format.
EDIT: at this point UNetbootin complained the USB device was unmounted. Keeping UNetbootin open I ran sudo mount /dev/sdb1 /mnt. Then I hit "OK" on the still-open UNetbootin. It wrote the USB image to the USB.

Format drive to NTFS, How?
Now to create a bootable Windows 7 USB Drive while using Ubuntu, then you need to make sure you have a Windows 7 .ISO file (you can create it from the DVD) and a 4GB USB flash drive (or larger).
Install Gparted and format the USB drive to NTFS. In Ubuntu, use the following command to install Gparted:1
sudo apt-get install gparted
To be able to format a drive to NTFS, you'll also need ntfs-3g - install it using the following command:1
sudo apt-get install ntfs-3g


Upgrading Ubuntu Kernel

Upgrading ubuntu kernel is easy; just needs a couple of steps:

#Update
sudo aptitude update
#search
sudo aptitutde linux-image
#upgrade
sudo aptitude safe-upgrade
#reboot
sudo init6

For more info
http://www.tcpdump.com/kb/os/linux/ubuntu-kernel-upgrade-procedure.html


Environment Variables

To define and load environment variables permanently in the system, we need to define it in the /etc/environment file as supersuer(since other users dont have permission to write the file). Then save it, after you save it, the newly defined environment variables are not loaded. Normally, they are loaded after you reboot it.

BTW, if you are hurry enough and do not have time to reboot the system, you just use

source /etc/environment

Thats it :)