My Quotes

When U were born , you cried and the world rejoiced
Live U'r life in such a way that when you go

Sunday, February 17, 2013

Connection Pool setting for JPA

Make sure that the following is set in the persistence.xml file

<!-- Important -->
<property name="hibernate.connection.provider_class"
  value="org.hibernate.connection.C3P0ConnectionProvider" />

<property name="hibernate.c3p0.max_size" value="100" />
<property name="hibernate.c3p0.min_size" value="0" />
<property name="hibernate.c3p0.acquire_increment" value="1" />
<property name="hibernate.c3p0.idle_test_period" value="300" />
<property name="hibernate.c3p0.max_statements" value="0" />
<property name="hibernate.c3p0.timeout" value="100" />

Ensure that the connection provider is set and all properties of connection pool are prefixed with "hibernate.

The easy way to verify is that you should see from the log files the following statements

INFO: Initializing c3p0-0.9.1 [built 16-January-2007 14:46:42; debug? true; trace: 10]
com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@f3a94e12 [     connectionPoolDataSource ->

Spring AOP cannot set value NULL

After hours of debugging I figured that indeed the issue was related to the various jar file configurations which was causing the trouble.

I have listed the ideal permutation which might help some one to fix this issue soon.

Ensure that the following are the appropriate versions atleast for SPRING related stuff.

  1. aopalliance-1.0.jar
  2. asm-3.1.jar
  3. asm-attrs-1.5.3.jar
  4. aspectjrt-1.6.5.jar
  5. aspectjtools-1.6.9.jar
  6. aspectjweaver-1.6.5.jar
  7. cglib-nodep-2.1_3.jar
  8. commons-logging-1.1.1.jar
  9. log4j-1.2.16.jar
  10. spring-aop-3.1.2.RELEASE.jar
  11. spring-asm-3.1.2.RELEASE.jar
  12. spring-aspects-3.1.2.RELEASE.jar
  13. spring-beans-3.1.2.RELEASE.jar
  14. spring-context-3.1.2.RELEASE.jar
  15. spring-core-3.1.2.RELEASE.jar
  16. spring-expression-3.1.2.RELEASE.jar
  17. spring-instrument-3.1.2.RELEASE.jar
  18. spring-tx-3.1.2.RELEASE.jar

Tomcat JNDI Exception DataSource

·         DO NOT configure the DataSource (or) EntityManagerFactory in the Spring beans.
·         The version of Tomcat SHOULD BE minimum of 7.0.25. For lower versions of Tomcat you still need to use the regular connection like driverà url à password etc., This is a BUG with Tomcat server itself.
·         If you try to use DataSource with earlier versions of the Tomcat, then you will see JNDIExceptions
·         Since Tomcat does not support JTA , you need to have the following configuration for the datasource in your persistence.xml file
<persistence-unit name="<<Name>>"  transaction-type="RESOURCE_LOCAL">   
   <!--GLOBAL_JNDI_GOES_HERE This is for Tomcat which does not support JTA-->
   <non-jta-data-source>java:comp/env/jdbc/<<Your DataSource Name>></non-jta-data-source>
·         For hibernate way it will be the following
<property name="hibernate.connection.datasource">java:comp/env/jdbc/<<Your DataSource Name>></property>
·         Note that for Tomcat you need to make sure that you are reading the datasource as a “resource” and that’s why you need have the java:compo/env as a prefix for the datasource.
·         Now normally you configure the resources in either.
a.       <Tomcat_Home>\Conf\context.xml (or)
b.      <Tomcat_Home>\Conf\server.xml (or)
·         But once this is done as above the whole of Tomcat applications will have access to that resource which we do not want to do. This also means that that you need to have database specific driver files loaded and visible to all the applications deployed.
·         EPH proposes to use application specific Context files. Here are the steps for the same.
·         Create a file called “context.xml” in your webapp folder
a.       WebContent\META-INF
·         The contents of this file is as follows
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/<<Your DataSource Name>>">
        <Resource name="jdbc/<<Your DataSource Name>>"
·         Note the equality in “name” and “path” in the above file.
This way your DB driver files are loaded at runtime (WEB-INF\lib) and not at server start up time.

Hibernate 4 and Configuration

If you use 
configuration = new Configuration();

You will see the warning 
configuration.configure is deprecated.

To get rid of this do the following

Configuration configuration =null;
ServiceRegistry serviceRegistry =null;
configuration = new Configuration();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
factory = configuration.buildSessionFactory(serviceRegistry);

This will get rid of the warning

Wednesday, February 6, 2013

Caused by: java.sql.SQLException: Io exception: Got minus one from a read call

I was struggling with the following exception for couple of days and here is my fix
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Could not open connection
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Could not open connection

Caused by: java.sql.SQLException: Io exception: Got minus one from a read call

Make sure that you DO NO SPAN a transaction in any of the READ methods.

If so ensure that before you call the  entitymanager.close(), do a 

This might prove costly because of the COMMIT. But on the contrary if you do not commit / rollback, then the transaction will be OPEN
and trying to close the EntityManager will not close the EM since
em.getTransaction().isActive() is true.

so these connections will be open causing the avbove said exception over a period of time.

So better the choice of execution order would be

}catch(Exception ){ do something}

One last thing, closing the EMF will actually close the connection pool.

I really liked this website giving more insight and information about JPA

Generate JAXB with individual packages using MAVEN

If you want to generate JAXB using MAVEN from multiple XSD files
and also ensuring that the package for each XSD is different.
Following lines of code will let you do that with ease