Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Wednesday, 22 April 2020

Strping jdbcTemplate DB connection not closing or not getting new connections issue solution


Add below TransactionManger framework to resolve the DB connections issue.

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg>
<ref bean="dataSource" />
</constructor-arg>
</bean>

<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

Tuesday, 21 April 2020

String contains special characters or not checking in Java?


By using below Java code you find out string contains a special character or not?
package com.javapages4all;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StrRegCheck {

public static void main(String[] args) {

String name="ra2ja2 3ABC 123";
Pattern special= Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
Matcher matcher = special.matcher(name);

if(matcher.find()){
//string contains special symbol/character
System.out.println("string having special character");
} else{
System.out.println("string don't have any special character");
}
}
}

Saturday, 11 April 2020

Java Spring Quartz or CronTrigger loading multiple times Solution


Java Spring Quartz or CronTrigger loading multiple times with below Solution we can make load it once.

Spring Quartz or CronTrigger loading multiple times and its not problem with Quartz or CronTrigger configuration. this issue occurring because of the below "Web.xml" configuration issue .

1. Spring MVC Configuration(Web.xml).

 <servlet>
    <servlet-name>sampleapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>sampleapp</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

In Above Web.xml Spring configuration we have to observe/change the below things.

1. The servlet "sampleapp" will load "applicationContext.xml/sampleapp-servlet.xml" file.
2. And the listener ContextLoaderListener will loads "sampleapp-servlet.xml" again.

To fix the above issues do the below changes in web.xml

Fix: 1. change the servet-name "sampleapp" to some other name.
2. change the "applicationContext.xml" to "projectname-servlet.xml", 
i.e: "javapages" is my project name.

Please find the below spring configuration web.xml after above changes.

 <servlet>
    <servlet-name>testapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/javapages-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>testapp</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

Java Spring MVC beans loading twice Solution


Java Spring MVC beans loading twice with below Solution we can make load it once.


1. Spring MVC Configuration(Web.xml).


 <servlet>
    <servlet-name>sampleapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>sampleapp</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

In Above Web.xml Spring configuration we have to observe/change the below things.

1. The servlet "sampleapp" will load "applicationContext.xml/sampleapp-servlet.xml" file.
2. And the listener ContextLoaderListener will loads "sampleapp-servlet.xml" again.


To fix the above issues do the below changes in web.xml

Fix: 1. change the servet-name "sampleapp" to some other name.
2. change the "applicationContext.xml" to "projectname-servlet.xml", 
i.e: "javapages" is my project name.

Please find the below spring configuration web.xml after above changes.

 <servlet>
    <servlet-name>testapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/javapages-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>testapp</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>


Tuesday, 8 July 2014

Solution for AJAX Repeat Calls

The AJAX Repeat Calls(caching) is due to the same URL that's being called repeatedly.
If you change the URL dynamically then this issue can be resolved.
  1. Something like by adding a query String with the current time with the request                            ( or )
  2. any random generated number
You can change the URL without affecting the result.