Wednesday, 31 October 2018

Why is Hibernate Optimistic Locking Failure Exception thrown when saving a new entity?

org.springframework.orm.ObjectOptimisticLockingFailureException: Object of class [com.payumoney.paymentUtil.model.EntityTDR] with identifier [389853]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.payumoney.paymentUtil.model.EntityTDR#389853]

Solution :
In the Entity or Model if we are using version column and if we are not mentioned below annotation then we will get this issue

@Generated(GenerationTime.ALWAYS)

Key points:
  1. The relationship between the parent and child is bi-directional one-to-many.
  2. We use optimistic locking with the version column being a timestamp created by MySQL either during insert or during update. On the version field we specify Generated(GenerationTime.ALWAYS) to ensure that the version details are obtained from the database automatically (avoid the time precision issue between Java and MySQL)
  3. During saving a new entity (id = 0), I can see the logs that the entity is being inserted into the database, I can also see the child entities being inserted in the database (via the Hibernate logs). During this process, I can also see the a select is done to get the version details from the database.
  4. Soon after the entities are inserted and the session is being flushed, there is a dirty checking is done on the collection and I see a message in the log that the collection is (). Straight after this, I see an update statement on the parent entity’s table and this is where the problem occurs as the version value used in the update statement is different to what is in the database, the exception is thrown.

Tuesday, 27 March 2018

How to remove the middle of the commit from git local

Rebase or revert are the options. Rebase will actually remove the commit from the history so it will look like that second commit never existed. This will be a problem if you've pushed the master branch out to any other repos. If you try to push after a rebase in this case, git will give you a reject non fast-forward merges error.

Revert is the correct solution when the branch has been shared with other repos. git revert af5c7bf16 will make a new commit that simply reverses the changes that af5c7bf16 introduced. This way the history is not rewritten, you maintain a clear record of the mistake, and other repos will accept the push.

Here's a good way to erase: git rebase -i <commit head>^ That takes you to the commit just before the one you want to remove. The interactive editor will show you a list of all the commits back to that point. You can pick, squash, etc. In this case remove the line for the commit you want to erase and save the file. Rebase will finish its work.

Monday, 13 November 2017

javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated ???

If you are getting javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated when you are calling any API in server to server call.

Step 1:
Download the bouncy castle jars according to your Java version using below URL
https://www.bouncycastle.org/latest_releases.html

Step 2: Copy downloaded jars (bcpkix-jdk15on-1.50.jar, bcprov-jdk15on-1.50.jar) into your JRE ext directory.
Example - If you are installed java in C drive
C:\Java-JDK-1.6.0.45\Java\jdk1.6.0_45\jre\lib\ext\

Step 3: Open java.security file available in below path if you are installed java in C drive

C:\pavan\Java-JDK-1.6.0.45\Java\jdk1.6.0_45\jre\lib\security\java.security

add the below command end of the file java.security.
security.provider.2=org.bouncycastle.jce.provider.BouncyCastleProvider

Step 4: 
Use below method to overcome the same while creating the HttpClient object.

Create the HttpClient object using below code and call the API you will get result 100%!!!

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
......
private static HttpClient getHttpClient() {

    try {
        SSLContext sslContext = SSLContext.getInstance("SSL");

        sslContext.init(null,
                new TrustManager[]{new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() {

                        return null;
                    }

                    public void checkClientTrusted(
                            X509Certificate[] certs, String authType) {

                    }

                    public void checkServerTrusted(
                            X509Certificate[] certs, String authType) {

                    }
                }}, new SecureRandom());

        SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext,SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);



        HttpClient httpClient = HttpClientBuilder.create().setSSLSocketFactory(socketFactory).build();

        return httpClient;

    } catch (Exception e) {
        e.printStackTrace();
        return HttpClientBuilder.create().build();
    }
}

Tuesday, 10 October 2017

how to remove untracked files from git local

  1. To remove directories, run git clean -f -d or git clean -fd.
  2. To remove ignored files, run git clean -f -X or git clean -fX.
  3. To remove ignored and non-ignored files, run git clean -f -x or git clean -fx.

Wednesday, 28 December 2016

How to change the alias name of JKS file!!!!

Hi All

How to change the alias name of JKS file once we generated?

Step1 : To view what alias is available present in JKS file, use the below command

Open the command prompt and got the location where the jks file is available and execute below command
keytool -list -v -keystore <jks file name>

Example:
keytool -list -v -keystore test.jks

Step2 : once you executed the above command it will ask you the jks password, enter the same then it will display all the details like what all are certificated available in jks, expiry of certificates, alias name..etc..

Step3 : Execute the below command to change the alias name

keytool -keyclone -alias "old alias name" -dest "new alias name" -keystore <jks file>

Example :
keytool -keyclone -alias "pavan" -dest "kumar" -keystore test.jks

once you have execute above command it will ask you the password of jks, enter the same.
Done..!!!!enjoy..


How to change the certificate password

Hi All, after a long time posting again

How to change the certificate password using keytool command

Step1: open to the command prompt and go to the location where the certificate exists.
Step2:  execute the below command

keytool -storepasswd -keystore <jks file name>

Example
keytool -storepasswd -keystore test.jks

Step3: it will ask you the current password enter the same.
Step4: it will ask you the new password enter the new password.
Step5: it will ask you the password again for confirmation enter the same password which is entered  
           in step5.
Step6: thats it!!! your jks file got changed with new password....enjoy!!!!

Tuesday, 2 February 2016

How to use equals( ) and equalsIgnoreCase( ) in Java ?

To compare two strings for equality, use equals( ). It has this general form:
boolean equals(Object str)
Here, str is the String object being compared with the invoking String object. It returns true if the strings contain the same characters in the same order, and false otherwise.
The comparison is case-sensitive. To perform a comparison that ignores case differences, call equalsIgnoreCase( ). When it compares two strings, it considers A-Z to be the same as a-z. It has this general form:
boolean equalsIgnoreCase(String str)

Here, str is the String object being compared with the invoking String object. It, too, returns true if the strings contain the same characters in the same order, and false otherwise. Here is an example that demonstrates equals( ) and equalsIgnoreCase( ):

In one word we can say equals() is case-sensitive, equalsIgnoreCase() is not a case-sensitive.

Below is the example
// Demonstrate equals() and equalsIgnoreCase().
class equalsDemo {
        public static void main(String args[]) {
               String s1 = "Hello";
               String s2 = "Hello";
               String s3 = "Good-bye";
               String s4 = "HELLO";
               System.out.println(s1 + " equals " + s2 + " -> " +
               s1.equals(s2));
               System.out.println(s1 + " equals " + s3 + " -> " +
               s1.equals(s3));
               System.out.println(s1 + " equals " + s4 + " -> " +
               s1.equals(s4));
               System.out.println(s1 + " equalsIgnoreCase " + s4 + " -> " +
               s1.equalsIgnoreCase(s4));
       }
}

The output from the program is shown here:
Hello equals Hello -> true
Hello equals Good-bye -> false
Hello equals HELLO -> false
Hello equalsIgnoreCase HELLO -> true