Menu

Saturday 16 February 2019

java.sql.SQLRecoverableException: Closed Connection

We are getting below exception “java.sql.SQLRecoverableException: Closed Connection” error on JBoss instance.

Error log:
*********************************************************************************
 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/WebApp].[WebApp]] (http--0.0.0.0-8080-1)
Servlet.service() for servlet WebApp threw exception: java.sql.SQLRecoverableException: Closed Connection
at oracle.jdbc.driver.PhysicalConnection.commit(PhysicalConnection.java:3817) [ojdbc6.jar:11.2.0.2.0]
at oracle.jdbc.driver.PhysicalConnection.commit(PhysicalConnection.java:3857) [ojdbc6.jar:11.2.0.2.0]
at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.jdbcCommit(BaseWrapperManagedConnection.java:986)
at org.jboss.jca.adapters.jdbc.WrappedConnection.commit(WrappedConnection.java:757)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doCommit(JdbcTransaction.java:112) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:178) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:554) [spring-orm-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755) [spring-tx-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724) [spring-tx-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475) [spring-tx-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:270) [spring-tx-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94) [spring-tx-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631) [spring-aop-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at  
*********************************************************************************


What is java.sql.SQLRecoverableException: Closed Connection?

Above exception is due to one of these reasons this may due to the lower version of Oracle [ojdbc6.jar], network issue, firewall timeout configuration or database timeout.
The timeout should properly check or configured in JBoss, the value must be greater than 0 but less than timeout has been used in a database, network firewall, etc.

But as per my case, Oracle [ojdbc6.jar] is lower version as per database server required.


How to check ODJBC.jar version using the command line?

Command:   java  -jar  ojdbc6.jar  -version



Solution: We should use the same Oracle database JDBC driver version as per the database version.


Thanks :-)

Thursday 14 February 2019

java.lang.VirtualMachineError: out of space in CodeCache for adapters


Code Cache issue:

We are getting below exception “java.lang.VirtualMachineError: out of space in CodeCache” error on JBoss instance

Error log:
*********************************************************************************
 11:19:08,460 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/WebApplication].[default]] (ajp-/192.168.100.171:8009-1) Servlet.service() for servlet default threw exception: java.lang.VirtualMachineError: out of space in CodeCache for adapters
at java.lang.Class.getDeclaredConstructors0(Native Method) [rt.jar:1.6.0_35]
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389) [rt.jar:1.6.0_35]
at java.lang.Class.getDeclaredConstructors(Class.java:1836) [rt.jar:1.6.0_35]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:229) [spring-beans.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:962) [spring-beans.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:935) [spring-beans.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485) [spring-beans.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) [spring-beans.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:313) [spring-beans.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) [spring-beans.jar:3.1.1.RELEASE]
at 
*********************************************************************************

What is CodeCache ??
  
The Java Virtual Machine (JVM) generates native (compiled) code and stores it in a memory area called the codecache. The code cache is where jar definitions are stored.
The default maximum size of the CodeCache on most of the platforms is 48M. If any application needs to compile a large number of methods resulting in a huge amount of compiled code then this CodeCache may become full. 
You can set the code cache size by setting the property -XX:ReservedCodeCacheSize and monitor it by tools like JConsole. When the code cache becomes full, the Java VM flushes and sweeps it. In Java 7 and higher, code cache flushing is enabled by default.

Codecache Size Options:-

Option
Default
Description
InitialCodeCacheSize
160K (varies)
Initial code cache size (in bytes)
ReservedCodeCacheSize
32M/48M
Reserved code cache size (in bytes) - maximum code cache size
CodeCacheExpansionSize
32K/64K
Code cache expansion size (in bytes)


Steps to check CodeCache Size in JBOSS:

Step 1: Locate to $JBOSS_Home/bin

Step 2: Command to connect JBoss instance :

            Command jboss-cli.bat -c --controller=localhost:9999

Step 3: Command to check CodeCache size in JVM.

            Command:    /core-service=platform-mbean/type=memory-pool/name=Code_Cache/:read-resource(recursive=true,proxies=true,include-runtime=true,include-defaults=true)

Step 4: Output as follows:

{
    "outcome" => "success",
    "result" => {
        "name" => "Code_Cache",
        "type" => "NON_HEAP",
        "valid" => true,
        "memory-manager-names" => ["CodeCacheManager"],
        "usage-threshold-supported" => true,
        "collection-usage-threshold-supported" => false,
        "usage-threshold" => 0L,
        "collection-usage-threshold" => undefined,
        "usage" => {
            "init" => 2555904L,
            "used" => 99788096L,
            "committed" => 100663296L,
            "max" => 100663296L
        },
        "peak-usage" => {
            "init" => 2555904L,
            "used" => 99797824L,
            "committed" => 100663296L,
            "max" => 100663296L
        },
        "usage-threshold-exceeded" => true,
        "usage-threshold-count" => 0L,
        "collection-usage-threshold-exceeded" => undefined,
        "collection-usage-threshold-count" => undefined,
        "collection-usage" => undefined
    }
}


Step 5: Increase a CodeCache size by “-XX:ReservedCodeCacheSize=128M” in  JBOSS JVM  argument.


Reference URL :


    


Tuesday 12 February 2019

Apache Rewrite Rules


  • Apache provides a REWRITE RULE function to redirect the requests from one URL/Page to another URL/Page.
  • To use this functionality we have to enable the mod_rewrite module in apache web server.
  • mod_rewrite provides a flexible and powerful way to manipulate URLs using an unlimited number of rules. 
  • mod_rewrite operates on the full URL path, including the path-info section. A rewrite rule can be invoked in httpd.conf or in .htaccess file.

Enable module:
  • LoadModule rewrite_module modules/mod_rewrite.so


NOTE: We should not always use .htaccess file for large number of rewrite rule redirection, since which may leads to the slowness of Apache server.


#####Forward/Rewrite , main domain towards application context.

RewriteEngine on
RewriteRule   ^/$     /CONTEXT/ [R]



RewriteRule Basics
A RewriteRule consists of three arguments separated by spaces. The arguments are
  • Pattern: which incoming URLs should be affected by the rule;
  • Substitution: where should the matching requests be sent;
  • [flags]: options affecting the rewritten request.



The Substitution can itself be one of three things:

A full filesystem path to a resource
  • RewriteRule "^/games"  "/usr/local/games/web"

This maps a request to an arbitrary location on your filesystem, much like the Alias directive.


A web-path to a resource
  • RewriteRule "^/foo$"  "/bar"

If DocumentRoot is set to /usr/local/apache2/htdocs, then this directive would map requests for http://example.com/foo to the path /usr/local/apache2/htdocs/bar.


An absolute URL

  • RewriteRule "^/product/view$"  "http://site2.example.com/seeproduct.html" [R]



Rewrite Conditions



For example, to send all requests from a particular IP range to a different server, you could use:

RewriteCond  "%{REMOTE_ADDR}"  "^10\.2\."
RewriteRule  "(.*)"  http://intranet.example.com%241/



Examples:

Redirect All Website Pages

# Redirect all pages from olddomain.com # to newdomain.com
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^olddomain.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]



Meta Refreshes:

This method uses a special Meta tag in the HTML source to control the redirect. In the early days of the Internet, this was the main method of generating redirects.
The meta refresh should appear within the head section of the HTML source.

An example is:

<meta http-equiv="Refresh" content="0; url=http://www.example.com/" />

The number (0 in this example) refers the the number of seconds to wait before performing the redirect. A zero second delay results in an instant redirect and is the equivalent of doing a 301 permanent redirect.

Reference link:



Redirect rule for old page to new page:

RewriteEngine On
Redirect permanent   /page1.html   http://example.com/newpage1.html
Redirect permanent   /folder/page2.html  http://example.com/newpage2.html

Reference link:



 Redirect a portion of your site to HTTPS, you might do the following:

<If "%{SERVER_PROTOCOL} != 'HTTPS'">
    Redirect "/admin/" "https://www.example.com/admin/"
</If>



Redirect all requests from the www to the non-www version of the domain, or vice-versa:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*)$ "http://example.com/$1" [R=301,L]

Reference link:



PHP Redirects :
If you want to do a PHP 301 permanent redirect, you just need to add the redirect code, ie:

<?php
Header('Location: http://example.com/newpage',TRUE,301);
?>

Reference link:



Rewriting from Old to New (external):
Solution:
We force a HTTP redirect to the new URL which leads to a change of the browsers and thus the users view:

RewriteEngine  on
RewriteRule    "^/foo\.html$"  "bar.html"  [R]

Reference link:



Redirect all pages from Http to Https:

<VirtualHost *:80>
  ServerName www.example.com
  ServerAlias example.com www.new-example.com

  Redirect "/" "https://www.example.com/"
</VirtualHost>

 OR

RewriteEngine On
RewriteRule ^/$  https://%{HTTP_HOST}/ [R,L]
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R,L]
 
 OR

RewriteEngine On
RewriteCond %{HTTPS}  !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L



Redirect all pages from Https to Http:

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{SERVER_PORT} ^443$ [OR]
        RewriteCond %{HTTPS} =on
        RewriteCond %{HTTP_HOST} !^vhost2.test.com$
        RewriteRule ^/(.*) http://%{HTTP_HOST}/$1 [L,R]
    </IfModule>



Redirect a URL to www.example.com:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com$1 [R=301,L]



Redirect request from url https://10.20.40.50 to https://www.domain.example.com.

<VirtualHost _default_:443>
ServerName www.domain.example.com
RewriteEngine on
RewriteCond %{SERVER_PORT} ^443$ [OR]
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} !^www.domain.example.com$
RewriteRule ^/(.*) https://www.domain.example.com/$1 [L,R]
</VirtualHost> 



Alias & AliasMatch  Directive:

The Alias directive allows documents to be stored in the local filesystem other than under the DocumentRoot.

Alias "/image" "/ftp/pub/image"
                &
Alias "/icons/" "/usr/local/apache/icons/"

Note that if you include a trailing / on the URL-path then the server will require a trailing / in order to expand the alias

A request for http://example.com/image/foo.gif would cause the server to return the file /ftp/pub/image/foo.gif.

Reference link:



Logging:
mod_rewrite offers detailed logging of its actions at the trace1 to trace8 log levels.

Example:
LogLevel alert rewrite:trace3

To check the logs :
tail -f error_log|fgrep '[rewrite:'


Redirect Detective is a free redirect checker

Reference links:


Thanks :-)