Friday, 26 June 2015

Java Hibernate : hibernate-could-not-obtain-transaction-synchronized-session-for-current-thread

Some times with hibernate we got error while fetching data from database.

Error like :

hibernate-could-not-obtain-transaction-synchronized-session-for-current-thread

For this error one of the simple solution is :

Just user @Transactional annotation in your DAO implementation method from where you got error.

For Example :
        ...
        ...
        @Transactional
@Override
public List<Object> listObjectCategory(String objectName) {
...
....
}

Please put your valuable inputs and solutions to improving this post

Thursday, 18 June 2015

How to stop Tomcate server when it is not stop from eclipse

In some situation Apache Tomcat not stop from eclipse at that time we have to forcefully stop that server by using kill that process, Here we show this simple task that is very useful for developers.

This solution is tested and working file with Windows platform :

First of all we want to know that which process ID is for Apache tomcate port.

For that we follow bellow steps :
-> Open command promt  
-> Type command :
     netstat -o -n -a | findstr 8080
     Where 8080 is the PORT number on which Tomcat is start.
-> It will show you result like :
    TCP    0.0.0.0:8080                  0.0.0.0:0                       LISTENING         6852
    TCP    192.168.0.193:63473    192.168.0.100:8080     CLOSE_WAIT     6852
    TCP    [::]:8080                        [::]:0                             LISTENING         6852
    TCP    [::1]:8080                      [::1]:63536                   TIME_WAIT        0

-> Where the last number is ther Process ID that you have to kill for stooping the service on which Tomcat working. This number is differ every time.

-> For kill the process type the bellow command in command prompt :
    taskkill /F /PID 6852
    6852 is the process id that you get from first command.

-> This command will show you message like :
     SUCCESS: The process with PID 6852 has been terminated.

-> That it now you can restart Tomcat and it will start as normal way. Now enjoy the code.

KEEP VISITING THIS BLOG FOR MORE TECH DISCUSSION. 
YOUR SUGGESTION ARE WELCOMES FOR ANY POST.

Wednesday, 10 June 2015

How to convert Java object to / from JSON (Jackson)

Jackson is a High-performance JSON processor Java library. In this tutorial, we show you how to use Jackson’s data binding to convert Java object to / from JSON.
For object/json conversion, you need to know following two methods :
//1. Convert Java object to JSON format
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(new File("c:\\user.json"), user);
//2. Convert JSON to Java object
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(new File("c:\\user.json"), User.class);
Note
Both writeValue() and readValue() has many overloaded methods to support different type of inputs and outputs. Make sure check it out.

1. Jackson Dependency

Jackson contains 6 separate jars for different purpose, check here. In this case, you only need “jackson-mapper-asl” to handle the conversion, just declares following dependency in your pom.xml
  <repositories>
 <repository>
  <id>codehaus</id>
  <url>http://repository.codehaus.org/org/codehaus</url>
 </repository>
  </repositories>
 
  <dependencies>
 <dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-mapper-asl</artifactId>
  <version>1.8.5</version>
 </dependency>
  </dependencies>
For non-maven user, just get the Jackson library here.

2. POJO

An user object, initialized with some values. Later use Jackson to convert this object to / from JSON.
import java.util.ArrayList;
import java.util.List;
 
public class User {
 
 private int age = 29;
 private String name = "mkyong";
 private List<String> messages = new ArrayList<String>() {
  {
   add("msg 1");
   add("msg 2");
   add("msg 3");
  }
 };
 
 //getter and setter methods
 
 @Override
 public String toString() {
  return "User [age=" + age + ", name=" + name + ", " +
    "messages=" + messages + "]";
 }
}

3. Java Object to JSON

Convert an “user” object into JSON formatted string, and save it into a file “user.json“.
import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
 
public class JacksonExample {
    public static void main(String[] args) {
 
 User user = new User();
 ObjectMapper mapper = new ObjectMapper();
 
 try {
 
  // convert user object to json string, and save to a file
  mapper.writeValue(new File("c:\\user.json"), user);
 
  // display to console
  System.out.println(mapper.writeValueAsString(user));
 
 } catch (JsonGenerationException e) {
 
  e.printStackTrace();
 
 } catch (JsonMappingException e) {
 
  e.printStackTrace();
 
 } catch (IOException e) {
 
  e.printStackTrace();
 
 }
 
  }
 
}
Output
{"age":29,"messages":["msg 1","msg 2","msg 3"],"name":"mkyong"}
Note
Above JSON output is hard to read. You can enhance it by enable the pretty print feature.

4. JSON to Java Object

Read JSON string from file “user.json“, and convert it back to Java object.
import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
 
public class JacksonExample {
    public static void main(String[] args) {
 
 ObjectMapper mapper = new ObjectMapper();
 
 try {
 
  // read from file, convert it to user class
  User user = mapper.readValue(new File("c:\\user.json"), User.class);
 
  // display to console
  System.out.println(user);
 
 } catch (JsonGenerationException e) {
 
  e.printStackTrace();
 
 } catch (JsonMappingException e) {
 
  e.printStackTrace();
 
 } catch (IOException e) {
 
  e.printStackTrace();
 
 }
 
  }
 
}
User [age=29, name=mkyong, messages=[msg 1, msg 2, msg 3]]