ThreadLocal

ThreadLocal

What is ThreadLocal?
There are different scope of a variable in java.
1. Local Scope : This scope includes the variable declared inside the methods.
2. Instance Scope: This scope is also known as instance variable. This is created one per instance.
3. Class Scope: The variable is created one per class. They are generally static
4. Thread Scope: The variable is available in thread scope using ThreadLocal.

ThreadLocal allows the java developer to store any variable or data in Thread scope. The treadlocal will take care that the data is only visible to thread. ThreadLocal thus helps to manage threadsaftey for an object/pojo across the thread without using the synchronous keyword.

How to keep data in ThreadLocal?
ThreadLocal provides set() method to store the data, and get() method to retrive the data. Using the getter and setter the manipulation can be done quite easily.

Generally ThreadLocal instances are private static fields in classes (e.g.,we may associate a user ID or Transaction ID etc).
Frameworks generally use threadlocal to store context related data.
Using Generics in ThreadLocal.
After java 1.5 we can use Generic notation in the ThreadLocal declaration.
eg. ThreadLocal<String> threadLocal = new ThreadLocal<String>();

Mehods in ThreadLocal.
1. protected T initialValue() : This can be used to set the intial value of threadlocal other than null.
2. public T get() : This is the getter method
3. public void set(T value): Setter method to set the value
4. public void remove(): This method clears the data from the thread local

 

 

Sample Code to illustrate the ThreadLocal

package com.helical;

import com.helical.model.User;

public class ThreadLocalExample {

    private static ThreadLocal threadContext = new ThreadLocal();

    public User getUser() {
        User user = threadContext.get();
        if (user == null) {
            throw new UserDoesNotExistsException("There is no user inside the ThreadLocal");
        } else {
            return user;
        }

    }

    public void setUser(User user) {
        User localUser = threadContext.get();
        if (localUser != null) {
            throw new UserExistsException("There is already  a user in ThreadLocal");
        } else {
            threadContext.set(user);
        }
    }

    public void clearThreadLocal() {
        threadContext.remove();
    }

    private class UserExistsException extends RuntimeException {
        public UserExistsException(String s) {
        }
    }
}

JNDI

Java Naming and Directory Interface (JNDI)

Using JNDI, applications running in distributed environment, helps to look up services independently.

How JNDI is useful?

  1.  During deployment sequence, the same JNDI name can be used in different servers.
  2.  It helps to provide security of credential leakage to the user groups like developer, testers, stagers etc, because the credentials are configured in the server not in the application.
  3.  Connection Pooling can be achieved

How to use JNDI?
In order to use, JNDI classes and service providers should be there
The service provider may be :

  1. Lightweight Directory Access Protocol (LDAP)
  2. Common Object Request Broker Architecture (CORBA)
  3. Common Object Services (COS) name service
  4. Java Remote Method Invocation (RMI) RegistryThe objects needs to be created and registered so that , they can be further used for lookup.

How to configure JNDI in tomcat?
In tomcat environment we can configure JNDI in 3 different places
1. Inside the application using context.xml in META-INF directory.
2. Inside the tomcat server context.xml : Inside the apache-tomcat/conf directory we can configure. This can be helpful when multiple application needs the same database.
3. Both in server.xml and context.xml

NB: The database driver jar should be present in the tomcat lib directory

<Resource name="jdbc/lookupName" 
global="jdbc/lookupName" 
auth="Container" 
type="javax.sql.DataSource" 
driverClassName="com.mysql.jdbc.Driver" 
url="jdbc:mysql://localhost:3306/hdi" 
username="hiuser" 
password="hiuser" 

maxActive="100" 
maxIdle="20" 
minIdle="5" 
maxWait="10000"/>

In order to use it in java program we have to use the followng
DataSource ds = (DataSource) ctx.lookup(“java:/comp/env/jdbc/lookupName”);

Lucene Search

Introduction  to lucene api

  1. Lucene is a text search engine java api library.
  2.  It majorly helps in indexing and searching.
  3.  It searches by calculating a score for each document and gives the result based on the score that is more accurate.
  4. Document can be searched using the search phrase, wildcard characters , range query.
  5. Lucene is high-performance and scalable.

Some of the important classes in Lucene API are

Analyzer:- This abstract class main task is to extract tokens for indexing.
Some implementing classes are StandardAnalyzer.

IndexWriter:- The index-writer helps in creating indexes and write the indexes to file inside a directory that is passed as parameter.

Fields: A document can be broken down into multiple fields. The fields are attribute that can store some values.
Document:- This class contains collection of Fields. It helps in creating a virtual document. When looping through a directory whose files have to be indexed, we can construct the many fields object for a single file  and then put in the document object. This document object is then passed to the IndexWriter, which helps indexWriter to create indexes for that specific file.
IndexReader: This class can be used to open an existing indexes from indexDirectory and helps in searching with a user provided search pharase.
QueryParser: The query parser uses the file content as input and find a match.

Query : QueryParser returns a Query object after parsing the query-string.

To execute the query and find the actual document contents we pass the query object to IndexSearcher.search() method.

TopDocs object is returned from the IndexSearcher.search() method, it contains collection of the searched docs (scoreDocs)
ScoreDoc.:The scoreDoc can be used to convert the searched document to Indexed Doc.
The pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.helical.lucene</groupId>
    <artifactId>helical.lucene.search</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-core</artifactId>
            <version>6.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-queries</artifactId>
            <version>6.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-queryparser</artifactId>
            <version>6.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-analyzers-common</artifactId>
            <version>6.1.0</version>
        </dependency>
    </dependencies>


</project>

LuceneIndexingAndSearchingExamples.java

import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;

public class LuceneIndexingAndSearchingExamples {

    private static final String indexStoreDirectory = "E:/search/indexes";
    private static final String directoryToIndex = "E:/samples/documents";

    public static void main(String[] args) {
        this.indexDirectory();
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter your search phrase\n");
        String searchText = scanner.nextLine();
        this.searchFromIndexes(searchText);
    }

    private static void indexDirectory() {
        try {
            Directory directory = FSDirectory.open(Paths.get(indexStoreDirectory));
            IndexWriterConfig configuration = new IndexWriterConfig(new SimpleAnalyzer());
            IndexWriter indexWriter = new IndexWriter(directory, configuration);

            //Delete all indexes if exits
            indexWriter.deleteAll();
            File directoryToIndexFile = new File(directoryToIndex);
            processEachFile(indexWriter, directoryToIndexFile);
            indexWriter.close();
            directory.close();
        } catch (FileNotFoundException fne) {
            System.out.println("The sample folder is not found");
        } catch (IOException ioe) {
            System.out.println("There was some problem in i/o operation");
        }
    }

    private static void processEachFile(IndexWriter indexWriter, File directoryToIndexFile) throws IOException {

        File[] files = directoryToIndexFile.listFiles();
        if (files == null) {
            throw new RuntimeException("Directory " + directoryToIndex + " is empty/not found");
        }
        for (File eachFile : files) {
            Document virtualDocument = new Document();
            virtualDocument.add(new TextField("path", eachFile.getName(), Store.YES));
            StringBuilder builder = getContents(eachFile);
            virtualDocument.add(new TextField("contents", builder.toString(), Store.YES));
            indexWriter.addDocument(virtualDocument);
        }
    }

    private static StringBuilder getContents(File eachFile) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(eachFile)));
        StringBuilder builder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line).append("\n");
        }
        reader.close();
        return builder;
    }

    private static void searchFromIndexes(String text) {
        try {
            Path path = Paths.get(indexStoreDirectory);
            Directory directory = FSDirectory.open(path);
            IndexReader indexReader = DirectoryReader.open(directory);
            IndexSearcher indexSearcher = new IndexSearcher(indexReader);
            QueryParser queryParser = new QueryParser("contents", new StandardAnalyzer());
            Query query = queryParser.parse(text);
            TopDocs topDocs = indexSearcher.search(query, 10);
            printOutput(indexSearcher, topDocs);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    private static void printOutput(IndexSearcher indexSearcher, TopDocs topDocs) throws IOException {
        System.out.println("Total Matches found " + topDocs.totalHits);
        for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
            Document virtualDocument = indexSearcher.doc(scoreDoc.doc);
            System.out.println("Path of document : " + virtualDocument.get("path"));
            System.out.println("\n=================\nContents \n=================\n" + virtualDocument.get("contents"));
        }
    }
}

ref:
Apache Lucene 5.1.0 indexing and searching java example

Groovy Helpful Tips

Groovy Helpful Tips

Here are some useful information when using groovy

Single Line comment in Groovy
// We can comment using // in groovy as in Java.


Multi Line comment in Groovy
/* Multi-line comment style is same as in Java */


Documentation comment in Groovy
/**
* Similar to Java commenting Style
*/


Shebang style is helpful when running Groovy script directly from linux system
#!/usr/bin/env groovy


as and trait are keyword in Groovy


”’ Triple quoted string help to write strings in multiple line eg.
def multiLineString = ”’
line one
line two
line three
”’


String interpolation

def name = ‘Helical’ // a plain string
def greet = “Hello ${name}” //This will result in Hello Helical


List in groovy.

by default list are ArrayList
def numbers = [1, 2, 3]


To define a Linked List
def linkedList = [2, 3, 4] as LinkedList //Note the usage of as keyword


To define map

def capitalInfo = [state: ‘Delhi’, district: ‘South Delhi’, pin: ‘110019’]

capitalInfo[‘metroStation‘] = ‘Nehru Place’ //adds a new key in the map


Elvis operator
The “Elvis operator” is a shortening of the ternary operator.
userName = user.name ? user.name : ‘Guest’ //classic way
userName = user.name ?: ‘Guest’ //elvis way

Ways to execute python code from Java

There are many ways to execute Python code from with in Java. In case if your project has requirement to
execute Python code from Java, here are few code samples that I have collected from Internet.

Ways to use Python in Java Code

1. Using Jython ScriptContext:

Python code that is written in a file which can be executed using the Jython ScriptContext.
The following snippet uses the file named numbers.py and gets the output printed to the standard out put in python.

     StringWriter writer = new StringWriter();
     ScriptEngineManager manager = new ScriptEngineManager();
     ScriptContext context = new SimpleScriptContext();
     context.setWriter(writer);
     ScriptEngine engine = manager.getEngineByName("python");
     engine.eval(new FileReader("/home/numbers.py"), context);
     System.out.println(writer.toString());
     

2. Second way of using Jython:

In this approach PythonInterpreter object using which we can execute Python code.

        Properties properties = System.getProperties();
	PythonInterpreter.initialize(properties, properties, new String[0]);
	PythonInterpreter interp = new PythonInterpreter();
	interp.set("firstName", args[0]);
	interp.set("lastName", args[1]);
	interp.execfile("/home/numbers.py");
        

Another way of using PythonInterpreter

Using python code directly in the Java Programs. Here the python code is executed using the python interpreter written in Java.

        PythonInterpreter python = new PythonInterpreter();
	int number1 = 10;
	int number2 = 32;
	python.set("number1", new PyInteger(number1));
	python.set("number2", new PyInteger(number2));
	python.exec("number3 = number1 + number2");
	PyObject number3 = python.get("number3");
	System.out.println("Val : " + number3.toString());
        

3. Invoking native python interpreter using Java:

Using Runtime class or ProcessBuilder class of Java we can invoke the python interpreter directly
and pass the file that consists of the python code as an argument.
Here one should have Python installed on their machine and be available in the PATH.

      Process process = Runtime.getRuntime().exec("python numbers.py " + number1 + " " + number2);//OR
      ProcessBuilder builder = new ProcessBuilder("python", "test1.py", " " + number1, " " + number2);
      

Using ProcessBuilder API is more preferred approach than the Process.

Hope the samples have helped you in getting some insights.

Thanks,
Rajasekhar

Jenkins

JENKINS

What is..?

Jenkins is a tool (web application ) that helps in continuous integration of projects. It is open source and has capability to manage any types of build software. Jenkins can be seamlessly integrated with any testing and deployment environment.

Continuous Integration.

In software developement life cycle, there are various pahses of the software developement. In developement there may be checking in the code, modifying the code, testing, deployment , bug fixes etc. Continous Integration is a practise that requires developers to integrate code into a shared repository at regular intervals. The code can be checked in from various central repository such as GIT, SVN, CVS. They may be modified as per the requirements. Continious Integration is a practise that whenever a code commit occurs, the build process should be triggered.

How to get jenkins.

Jenkins can be downloaded from jenkins.io. Then it may be run from the command line using the java -jar command. It may be put in the application container such as tomcat, jboss, weblogics etc.

Once the Jenkins application is deployed in the machine, it has to be configured with the project repository, build tool (maven, ant etc) build time, testing configuration , email configuration etc.

After the above configuration is set , Jenkins create a disk-space to store all the projects, dependencies etc in a directory ~/.jenkins (default location). We may also change the location by setting the JENKINS_HOME environment variable before launching the servlet container.

 

Email Notification.

Jenkins also sends the email notification when any deployment event occurs. These email feeds can be managed in the email notification area. There is a provision to configure the SMTP settings for sending out emails.

 

Plugins

Jenkins ships in with many plugins to build project, manage project, add security, add user and organization level profile, etc We may also add plugins build for jenkins systems.

Manage Plugins screen helps to install, update and remove plugins.

Groovy

Groovy Language

  • Groovy can run on JVM
  • Groovy scripts can be executed using groovy jar file.
  • Annotations are supported in groovy.

Groovy installation.

  • Binary download link http://www.groovy-lang.org/download.html
  • The binary can be run from command prompt
  • Set GROOVY_HOME, GROOVY_HOME\bin variable as enviroment variable.
  • After installing and running in the command line you get the groovy shell(groovysh).
  • groovy> println ” hello helical”

Source code in groovy.

  • The extension for groovy source file is .groovy
  • We can write either script or we may also encapsulate the code as class definition inside this file.
  • By default groovy class is subclassed by java.lang.Object.
  • Java class and its objects can access groovy script class and object and vice versa.
  • Using groovyc we can compile the source code.
  • Plain Old Groovy Objects (POGO).

Intersting facts about groovy.

  • Groovy doesnot require ; at the end of statement.
  • Last expression of the method is returned by default. So we may not explicitly use return keyword.
  • Maps, list and regular expression readily available in groovy.
  • The following package are readily available. (groovy.lang.*, groovy.util.* , java.lang.*, j ava.util.*, java.net.*, java.io.*, java.math.BigInteger, java.math.BigDecimal)
  • Its classes and methods are by-default public
  • Automatic getter and setter is created for the fields in groovy class.
  • Unlike java == operator in groovy checks for contents.
  • The is() may be used to check if two variables/object refer the same.
  • The parameters inside a function are optional.
  • Gpath is expression language to evaluate nested structure.

Data types in Groovy.

    • The keyword def is used to define variable. We can also use specific type for variable declaration. Example int marks, String company. etc
    • Range data type is a Collection. Ranges consists of two values separated by two dots.
for (i in 0..9) {
  println ("Hello $i")
}

assert 'L'..'P' == ['L', 'M', 'N', 'O','P']

String & Gstring.

  • ‘Strings may be enclosed inside single quote ‘.
  • “Mehod() calls and ${variable} substitution inside a string is known as Gstring”.
  • ”’Tripple single quotes can be used for multi line string”’
  • “”” Multi line Gstring can be enclosed in tripple quotes”””
  • /regularexpression/

Methods in Groovy.

  • Groovy supports operator overloading. – operator can be used to substract string, << can be used for string concatination.
  • Groovy string has .toUrl() method that can be directly used to convert a string to Url encoding string.

 

 

package packageName
class ClassName {
  static void main(def args){
    def hetroList= [1,2,"hi","45"]
    hetroList.each{ println it }
  }
} 

Developing Custom Maven Plugins

Writing Custom Maven Plugins

In this article I would like to point you to a very handy concept i.e. writing custom maven plugins to facilitate in your build process.

If you are a Java developer then you must have been familiar with Maven. Basically maven is a build tool (but its developers say that it is more than that :)). It is a plugin execution engine that helps you write plugins, which can be executed in any maven life cycles.

Basically you will need to be aware of packaging modes, life cycles, build phases and goals of maven. If you are not aware of what all these are, you can find lots of online tutorials that give you good clarity.

Maven community has developed lots of plugins and for most of the use cases you may not need to write your own custom plugin. Sometimes your project build may have specific requiremts that does not get fulfilled by available plugins. In such case, if you have to write one – as I had to write – this tutorial helps you to get your hands dirty with some Java code to write plugins.

Just like POJO in java we have something known as MOJO – Maven plain Old Java Object.

What is a MOJO? (From apache documentation)

What is a Mojo? A mojo is a Maven plain Old Java Object. Each mojo is an executable goal in Maven, and a plugin is a distribution of one or more related mojos.

In short, a mojo is a maven goal, to extend functionality not already found in maven.

So, if you have to write a plugin you are supposed to write one or more mojos.

Here is a sample Mojo(Create a separate maven project):

package com.helicaltech;

/**
 * @author Rajasekhar
 * 
 * Developed at Helical IT Solutions
 */
@Mojo(name = "repack",
        defaultPhase = LifecyclePhase.PACKAGE,
        requiresOnline = false, requiresProject = true,
        threadSafe = false)
public class SampleMojo extends AbstractMojo {
    //The parameter basedir is configured in the plugin configuration
    @Parameter(property = "basedir", required = true)
    protected File basedir;

    //Required = true means it is mandatory to configure the xml
    @Parameter(property = "warName", required = true)
    protected String warName;

    public void execute() throws MojoExecutionException {

        //Write your code here that will get executed
    }
}

Here, name attribut of Mojo is the name of the goal that you would like to run.

The @Mojo annotation defines that the name of the goal is “repack” and that it is not thread safe. It also defines the default phase is the package phase. The last thing is that any member variable can become a parameter. This becomes a parameter for the plugin of a goal.

Example usage of the plugin in POM:

                <plugin>
                <groupId>com.helicaltech</groupId>
                <artifactId>helical-maven-plugin</artifactId>
                <version>${helical.maven.plugin.version}</version>

                <configuration>
                <!--Parameter for the plugin go here -->
                    <basedir>${project.basedir}</basedir>
                    <warName>${project.build.finalName}</warName>
                </configuration>

                <dependencies>
                    <!--You can include the dependencies of your plugins here -->
                    <dependency>
                        <groupId>org.zeroturnaround</groupId>
                        <artifactId>zt-zip</artifactId>
                        <version>1.9</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                    <!--During the package phase the goal repack will be executed -->
                        <phase>package</phase>
                        <goals>
                            <goal>repack</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

The groupId and the artifactId and the version numbers come from the POM file of your plugin.

Once you install the plugin to your maven local repository or your company’s shared central repository so that other developers can use your plugin.

Hope this article helped you and wish you happy coding!

–Rajasekhar
Helical IT Solutions

JAX-RS and Jersey api

JAX-RS and jersey

JSR 311 defines REST support in java. JAX-RS uses annotations to define the REST in java classes.

What is Jersey?

  • Jersey  provieds api/library for JSR 311. We can use it in implementing Restful webservices in a Java servlet container.
  • In server side it helps in scanning predefined classes and identifies RESTful resources. In web.xml we need to register the servlet for web application.
  • It also provies a client library to communicate with a RESTful webservice.

 

How does it works?
Let say the application has the following URL as the request from the client/browser.
http://localhost:8080/web-application/rest/rest_request_path

The servlet maps the required class and method to give correct response to the above request.
The annotations in the classes helps the servlet to pick the right mapping.

Using the internal library Java Architecture for XML Binding (JAXB) we can have the
JAX-RS supports the creation of XML and JSON.
Annotation Description 

Annotation  Description Package Detail
@GET helps in handle the GET request (javax.ws.rs.GET)
@POST helps in handling the POST request ( javax.ws.rs.POST)
@Produces it handles the output type that will be generated ( javax.ws.rs.Produces)
@Path it handles the request mapping for specific url ( javax.ws.rs.Path)
@PathParam helps to  inject values from the URL into a method parameter. (javax.ws.rs.PathParam)
@QueryParam if the request have any query parameters then we can use this to handle using this annotation (javax.ws.rs.QueryParam)
@Consumes it specifies MIME media types a REST resource can consume. (javax.ws.rs.Consumes)
@FormParam the REST resources generally consume XML/JSON. Using this we can read form parameters sent in POST requests. (javax.ws.rs.FormParam)
@PUT handles the PUT method (javax.ws.rs.PUT)
@DELETE handles the DELETE method (javax.ws.rs.DELETE)

 

Maven jersey dependency.
A quick google search will give you maven dependency. Depending upon the project requirement you can find one.

Maven dependency for jersey-servlet

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19</version>
</dependency>

Maven dependency for client
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.22.1</version>
</dependency>

 

Code snippets

/Maps the path for BaseURL/rest/myMapping
package com.helical.restExample

@Path("/myMapping")
public class SampleService {
   @GET
   @Produces(MediaType.TEXT_PLAIN)
   public String plainMessage() {
    return "REST WORLD";
   }

   @GET
   @Produces(MediaType.TEXT_XML)
   public String xmlMessage() {
     return "<?xml version=\"1.0\"?>" + "<message>REST WORLD" + "</message>";
   }


   @GET
   @Produces(MediaType.APPLICATION_JSON)
    public String jsonMessage() {
    return "{\"mesaage\" : \"rest world\"}";
  }


   @GET
   @Produces(MediaType.TEXT_HTML)
   public String htmlMessage() {
    return "<html> " + "<title>" + "REST WORLD" + "</title>"
     + "<body><h1>" + "Welcome to the Rest world" + "</body></h1>" + "</html> ";
   }

}

 

 

Registering the api

In the web.xml we need to configure the jersey servlet to help the mapping

<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.helical.restExample</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

 

This is good to go. We can run the webservice application to get the desired result.

Sources internet

 

Batch-Updation in Hibernate

Batch Updation in Hibernate

JDBC has long been offering support for DML statement batching. By default, all statements are sent one after the other, each one in a separate network round-trip. Batching allows us to send multiple statements in one-shot, saving unnecessary socket stream flushing.

Hibernate hides the database statements behind a transactional write-behind abstraction layer. An intermediate layer allows us to hide the JDBC batching semantics from the persistence layer logic. This way, we can change the JDBC batching strategy without altering the data access code.

Update code snippet look like this ,

Session session = sessionFactory.openSession();
Transaction txInstance = session.beginTransaction();
ScrollableResults studentInstance = session.createQuery("FROM STUDENT").scroll();
int count =0;
while( studentInstance.next())
{
   Student student =(Student) studentInstance.get(Student.class,StudentID); 
   student.setregNo( regNO );
   session.update(student);
  // 50 - size of batch which you set earlier.
  // For Detail "http://helicaltech.com/batch-insertion-in-hibernate/"
   if(++count %50==0)
   {
      session.flush();
      session.clear();
   }
}
txInstance.commit();
session.close();

----------------------------------

 

PUSHPRAJ KUMAR (BI Developer)