28 November 2008

Access denied Linux MySQL error solution

I have not worked much with Linux and today i installed MySQL on Ubuntu 8.10. I logged in using the command "mysql -u root -p password". I was happily logged into MySQL and was able to execute commands like "show databases".
Then tried to create a database called kilo using the command
create database kilo;
and was greeted with the error below.
ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'kilo'

The problem was in the space between -u and root. So the correct command to use is mysql -uroot -ppassword

26 November 2008

Skipping Test execution phase in Maven

In few scenarios we may want to skip the execution of tests in Maven. [For example when the execution of tests need special setup which may not be available to the build machine.]

we can use the following command which mentions the argument maven.test.skip to skip test execution phase.
mvn -Dmaven.test.skip=true clean install

24 November 2008

How to refer environment variables in ANT

We may have to access environment variables in our ANT build script. To do this, let us provide a handler to access the environment as below
<property environment="env"/>
Then you can refer any of the environment value like computer name on which the build script is run as below
<property name="destinationmachinename" value="dest.${env.COMPUTERNAME}"/>

18 November 2008

Validating connections in a pool in JBoss application server for MySQL

MySQL connections in a pool times out after a long period of inactivity [I believe 8 hours is the time period by default.] Hence the connections in the pool become stale and this results in exceptions like java.sql.SQLException: Communication link failure.(The same scenario can also be
simulated by bringing JBoss up when MySQL is up and running and then restarting MySQL server.) To avoid this we have to instruct JBoss to validate a connection by issuing a test SQL.

Option 1 and the preferred way to do it is to place the following in the datasource configuration file for JBoss
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE datasources
    PUBLIC "-//JBoss//DTD JBOSS JCA Config 1.5//EN"
    "http://www.jboss.org/j2ee/dtd/jboss-ds_1_5.dtd">

<datasources>
    <local-tx-datasource>
        <jndi-name>TestDB</jndi-name>
        <connection-url>jdbc:mysql://localhost:3306/seam</connection-url>

        <driver-class>com.mysql.jdbc.Driver</driver-class>
        <user-name>root</user-name>
        <password>password</password>
        <min-pool-size>4</min-pool-size>
        <max-pool-size>20</max-pool-size>
        <idle-timeout-minutes>10</idle-timeout-minutes>
        <exception-sorter-class-name>
    com.mysql.jdbc.integration.jboss.ExtendedMysqlExceptionSorter
        </exception-sorter-class-name>
        <valid-connection-checker-class-name>
    com.mysql.jdbc.integration.jboss.MysqlValidConnectionChecker
        </valid-connection-checker-class-name>
        <background-validation>true</background-validation>
        <background-validation-minutes>10</background-validation-minutes>
        <metadata>
            <type-mapping>mySQL</type-mapping>
        </metadata>

    </local-tx-datasource>

</datasources>

Option 2 - We can also calidate the connection by using the following 
   <check-valid-connection-sql>select 1</check-valid-connection-sql>
  <new-connection-sql>select 1</new-connection-sql> 

But JBoss documentation suggests that Option 1 is the preferred way.




16 November 2008

Maven java.lang.OutOfMemoryError

When we compile a project which has more Java classes we start getting OutOfMemory error. To solve this in Windows, please set the following environment variable MAVEN_OPTS with a sufficiently larger value for Xms and Xmx options for  JVM.

How to add entries in MANIFEST.MF using Maven in WAR

We may come across a situation where we need to add more entries to MANIFEST.MF in jar or war files other than the default entries. In Maven we have plug ins to accomplish the same.

In war files, we can use maven-war-plugin as below. Build number and the computer name will be added along with the default entries.
       <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
          <manifest>
            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
          </manifest>
          <archive>
            <manifestEntries>
              <Implementation-Build>${buildNumber}</Implementation-Build>
              <Build-Machine>${env.COMPUTERNAME}</Build-Machine>
            </manifestEntries>
          </archive>
        </configuration>
      </plugin>

</plugins>
   </build>

In EJB jar files we can use maven-ejb-plugin. For normal jars please use maven-jar-plugin in a similar fashion

 <plugin>
            <artifactId>maven-ejb-plugin</artifactId>
            <configuration>
               <archive>
                  <manifest>
                     <addClasspath>true</addClasspath>
                  </manifest>
                   <manifestEntries>
              <Implementation-Build>${buildNumber}</Implementation-Build>
              <Build-Machine>${env.COMPUTERNAME}</Build-Machine>
            </manifestEntries>
               </archive>
            </configuration>
         </plugin>

14 November 2008

Searching Files in Projects in NetBeans 6.1 plus

To search for files in projects in NetBeans 6.1 and 6.5, we can use Ctrl+O or Alt+Shift+O.
-Use Alt+Shift+O when we wish to search for any file type. 
-Use Ctrl+O when we wish to search only Java files. This will provide us the result faster than when using Alt+Shift+O as only Java files are considered.

Debug in Tomcat 6 plus using NetBeans 6.5

To enable remote debugging in Tomcat edit the Catalina.bat to include the following lines in the beginning of the file
set JPDA_TRANSPORT=dt_socket
set JPDA_ADDRESS=8000
set JAVA_HOME=E:\jdk6

 To start Tomcat in debug mode, please use the following command
catalina jpda start

Attaching the debugger from NetBeans is similar to that in Debugging in JBoss with Netbeans.
[Note:Remember to change the port number as required]

WAR or EAR deployment versus exploded folder deployment

When should we go for an archive [war or ear] deployment rather than exploded folder deployment in production environment?
I believe the following factors are driving forces to go for archive deployment [though servers like JBoss, Tomcat ultimately explode them into tmp/work directory]
  • Deployment in managed servers is easier [when we have clustering]
  • When we want to enforce that production bug fixes should go through a well defined process [I have seen few applications (deployed in exploded format) where the patch is applied in the production environment but never merged with the version control system]
  • When we have to deploy on a remote server [like using remote deployment tasks using ANT, Maven etc]
The only reason to go for exploded folder deployment is that applying patches for high priority bugs in production environment is easier.

Using finalName to control the archive name

In Maven, by the default the archive names [war, jar and ear] are derived from artifactId and version. To override and customize the name, we can make use of <finalName> as shown in the below snippet from pom.xml.

   <build>
        <finalName>mycustom_name</finalName>
        <plugins>
            <plugin>
            //Further code goes here

Speedup NetBeans 6.5 startup in windows

To speedup NetBeans 6.5 startup in windows, go to etc folder under NetBeans installation folder [for example on my machine it is E:\Program Files\NetBeans 6.5 RC2\etc] and open netbeans.conf

Edit the value of Xms to 256m as in the screen shot below.











Another way to speed up NetBeans is to deactivate plugins. In NetBeans menu, go to Tools--> Plugins and deactivate unused plugins one by one. [Choose the option to restart IDE later if you have more plugins to deactivate]

13 November 2008

How to read values from properties file in JBoss Seam

Let us assume we have two properties files namely  messages.properties and application.properties

In components.xml we place the below line
<core:resource-loader bundle-names="messages application"/>

Then in the Seam component where we want to retrieve the value for a given property (key), we inject the ResourceBundle as below.
   @In
    java.util.ResourceBundle resourceBundle;

The retrieval is done as below
String exhibitSaveLoc = resourceBundle.getString("exhibitsavelocation");

NetBeans code templates - expand shortcut auto completion

Eclipse users might me well aware of typing sysout and ctrl+space for System.out.println. In NetBeans 6.1 plus we can configure the same by selecting Tools-->Options in the menu.
Then select Editor and click on Code Templates tab. The screenshot is as below where Tab is configured as an equivalent for ctrl+space. If you scroll down you can notice that the equivalent of sysout is by default as sout

Logging static versus instance variable for Logger

This link provides good insight regarding usage of static versus instance variable for Logger
http://wiki.apache.org/jakarta-commons/Logging/StaticLog

How to find and list transitive dependency in Maven

Note: I have modified this post as per Brian's comment below.

Maven packs the transitive dependencies into the archive files. We can exclude specific transitive dependencies by using <exclusions>
To list transitive dependencies that are packed into the archive file, there is maven-dependency-plugin.

Invoke any of the maven life cycle phase like mvn install dependency:tree

To write the dependency content into a file, we can use
mvn install dependency:tree -outputFile=dependency.txt

A sample output is shown in the screenshot below

08 November 2008

Debugging in JBoss 4.2.x onwards using Netbeans 6.5 RC2 Plus

In this post let us discuss how to attach a Netbeans 6.5 RC2 plus debugger JBoss server 4.2.x plus.

First let us enable remote debugging in JBoss. Navigate to run.bat (under bin folder) and uncomment the highlighted line by removing rem

rem JPDA options. Uncomment and modify as appropriate to enable remote debugging.
rem set JAVA_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y %JAVA_OPTS%


Now start JBoss and JBoss will wait for the debugger instance to get attached after printing the below line.
Listening for transport dt_socket at address: 8787


Now in NetBeans menu choose Debug --> Attach Debugger as in the screen shot below.











In the popup window, select values as below and click Ok button. Thats it ..have a happy debugging 



Silent mode installation of Java

The below content in a batch file will install JDK in D:\jdk1_6_10 without popping up any GUI. Useful to install without user interaction. The log will be written into setuplog.txt
echo off
start ./sdk/jdk-6u10-windows-i586-p.exe /s INSTALLDIR=D:\jdk1_6_10  /L ./setuplog.txt