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






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

Friday, December 28, 2012

Load Properties dynamically

Use Apache Commons Configuration and you can re-load property file dynamically.

Here is the complete code snippet for you


Java Code

import java.util.Arrays;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;

public class ApacheCommonsTest {

private static final String PROPERTIES_FILE_NAME = "C:\\sampleconfig\\reloadconfig.properties"; // you can use your own folder for this

/**
* @param args
*/
public static void main(String[] args) {
ApacheCommonsTest classObj = new ApacheCommonsTest();
try {
classObj.getBackground();
classObj.getPieColors();
} catch (ConfigurationException e) {
e.printStackTrace();
}
}

private PropertiesConfiguration getPropertyConfig()
throws ConfigurationException {
PropertiesConfiguration config = null;
config = new PropertiesConfiguration(PROPERTIES_FILE_NAME);
config.setReloadingStrategy(new FileChangedReloadingStrategy());
config.setAutoSave(true);
return config;
}

private void readProperties() throws ConfigurationException {
PropertiesConfiguration config = getPropertyConfig();
config.load(PROPERTIES_FILE_NAME);
}

private void getPieColors() throws ConfigurationException {
readProperties();
String[] piecolors = getPropertyConfig().getStringArray("colors.pie");
System.out.println("colors.pie=" + Arrays.toString(piecolors));
}

private void getBackground() throws ConfigurationException {
readProperties();
String bgcolor = getPropertyConfig().getString("colors.background");
System.out.println("bgcolor=" + bgcolor);
}
}

Sample Property file
C:\\sampleconfig\\reloadconfig.properties


colors.pie=#FF0000,#00FF00,#0000FF,#123456
colors.background=#c53478


Jar files to run this program

You can get all the jar files from findjar.com

  1. commons-configuration-1.9.jar
  2. commons-lang-2.2.jar
  3. commons-logging-1.1.1.jar
Now no more reloading with server re-start


Thursday, December 27, 2012

Enable UML shapes in Visio


  • Stencil and Template for Visio 2010

Visio 2010 represents the first noticeable improvement in usability since Visio 2000, but no enhancement in the funtionality, upon which the UML 2.2 shapes are built upon. 
Therefore, the shapes for Visio 2010 are the same as for Visio 2007; there are minor changes in the template. 

Install: If you'd like the template to appear in the "Software and Database" category when you click File/New, together with Visio's own templates, 
create in any folder a subfolder called "Software and Database", for example, "...\My Documents\My Shapes\Software and Database". 

You could choose any folder, except of the Visio program folder, i.e., don't use C:\Program Files\Microsoft Office\Office14\Visio Content\1033. 

Unzip the stencils and template into "...\My Documents\My Shapes\Software and Database" or the folder you created. 

  1. Start Visio, 
  2. click the File tab, 
  3. click Options, 
  4. click Advanced, 
  5. and then, 
  6. under General at the very bottom, 
  7. click File Locations. 
  8. Type full path of this folder without the last segment "Software and Database" into the fields "Stencils" and "Templates". 
  9. That is, type in "C:\Document and Settings\<user name>\My Documents\My Shapes\" . 

The template "UML 2.2 Template (Visio 2010)" will appear in the category "Software and Database". 

If you'd like the UML 2.2 template to appear in another category, 
such as a "UML" category, use "UML" instead of "Software and Database" in the steps above.

The stencils and template are here: UML2.2-Visio2010.zip


  • Stencil and Template for Visio 2007

This stencil for Visio 2007 contains all the same shapes as the stencil for Visio 2003, 
as Visio 2007 has almost the same functionality as Visio 2003; there were only usability improvements.  

Install: If you'd like the template to appear in the "Software and Database" category 
when you click File/New, together with Visio's own templates, 
create in any folder a subfolder called "Software and Database", 
for example, "...\My Documents\My Shapes\Software and Database". 
You could choose any folder, except of the Visio program folder, 
i.e., don't use C:\Program Files\Microsoft Office\Office11\1033. 

Unzip the stencils and template into "...\My Documents\My Shapes\Software and Database" or the folder you created. 

  1. Start Visio, 
  2. click "Tools" 
  3. and "Options". 
  4. In the "Advanced" tab, 
  5. click "File Paths..." 
  6. and type full path of this folder, 
  7. without the last element "Software and Database" into the fields "Stencils" and "Templates", 
  8. that is insert "C:\Documents and Settings\<user name>\My Documents\My Shapes\".  
  9. Restart Visio. 
  10. The template "UML 2.2 Template (Visio 2007)" will appear in the category "Software and Database". 

If you'd like the UML 2.2 template to appear in another category, 
such as a "UML" category, use "UML" instead of "Software and Database" in the steps above.

The stencils and template are here: UML2.2-Visio2007.zip.