My Quotes


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






Sunday, February 17, 2013

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">   
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
   <!--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>>"
                          auth="Container"
                          type="javax.sql.DataSource"
                          driverClassName="oracle.jdbc.driver.OracleDriver"
                          url="jdbc:oracle:thin:@<<server>>:<<port>>:<<instance>>"
                          username="<<userid>>"
                          password="<<pwd>>"
                          maxActive="20"
                          maxIdle="30"
                          maxWait="-1"
        /> 
</Context>
·         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();
configuration.configure(<<hibernate.cfg.xml");

You will see the warning 
configuration.configure is deprecated.

To get rid of this do the following

Configuration configuration =null;
ServiceRegistry serviceRegistry =null;
try{
configuration = new Configuration();
configuration.configure(hibernateConfigFile);
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.
entitymanager.getTransaction().begin()

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

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

try{
     getEntityManager()
       READ
}catch(Exception ){ do something}
  finally{
    em.close();
}


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



 

Monday, January 28, 2013

Disable absolute URL access- Apache web server


you can disable accessing through absolute url and allow access only with relative url with this htaccess code 


RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]

Saturday, January 26, 2013

Persistence Unit not found - JPA- RESOLVED

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named

This is so annoying

I had been looking for solutions for the same. Here are the pointers for the same

Hope this helps someone.

  1. My project structure is as follows . I am using JPA2.0
  2. .
    ├── pom.xml
    └── src
        ├── main
        │   ├── java
        │   │   └── se
        │   │       └── mycomp
        │   │           ├── UserTest.java
        │   │           └── domain
        │   │               └── User.java
        │   └── resources
        │       ├── META-INF
        │       │   └── persistence.xml
        │       └── log4j.properties
        └── test
            └── java
  3. Compare the case sensitiveness for the Persistence Unit Name.
  4. If you have JPA2.0 as a dependency, you can go ahead and remove the same
  5. Check if your provider is 
  6. <persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
    Check your pom.xml if you have hibernate entity manager as a dependency.
    If needed add the hibernate annotations too . No HARM
       <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-entitymanager</artifactId>
          <version>${hibernate-core-version}</version>
        </dependency>
    
    
    Thats it, the errors are gone.

Sunday, January 6, 2013

Recursive windows commands


To do a recursive move do:
            for /r %x in (foo) do move "%x" "drive:\path\bar"
To do a recursive rename do:
            for /r %x in (*.c) do ren "%x" *.cpp