Showing posts with label exception. Show all posts
Showing posts with label exception. Show all posts

Java Mail network is unreacheable connect exception

Recently I stumbled upon a weird problem while debugging a simple java application that needed to send an email

Since I was debbuging this application locally I used the FakeSMTP tool that I described in this tutorial here : Testing email sending functionality with FakeSMTP which I run locally on my machine

So after editing the configuration on my Java program and firing up the SMTP server I was quite surprised to get the following exception :

Caused by: javax.mail.MessagingException: Could not connect to SMTP host: 127.0.0.1, port: 25 (java.net.SocketException: Network is unreachable: connect)
System.setProperty("java.net.preferIPv4Stack" , "true");

So once you are sure you problem is not coming from a non running SMTP server or a network connection, you can check if the culprit is no other than Java

If you read the Oracle documentation here you will see that by default Java prefers IPv6 to IPv4 which is the problem in our case since FakeSMTP binds on IPv4

Luckily there is a way to force Java to prefer IPv4 over IPv6 using the system property java.net.preferIPv4Stack

This can either be done :

  • programmatically
  • using java options

1. Programmatically


 System.setProperty("java.net.preferIPv4Stack" , "true");

2. Using java options


java -jar myJar.jar -Djava.net.preferIPv4Stack=true

That should take care of the problem

Another solution would be to disable IPv6 networking on your machine but that may have other side effects on your system..

EclipseLink (JPA) Spring - No persistence exception translators exception

I recently stumbled upon a Spring Exception while working with Spring Data JPA and attempting to deploy a WAR file on glassfish. I have a JTA datasource defined in my glassfish which is used by Spring (through JNDI) to instantiate my entity manager. The JPA configuration was pretty straight forward but every time I tried to deploy the app on Glassfish I stumbled upon the error :
org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 
'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0': 
Initialization of bean failed; 
nested exception is java.lang.IllegalStateException: 
No persistence exception translators found in bean factory.
Cannot perform exception translation.
Apparently when using Hibernate there is an easy fix for this : Declaring a Hibernate Exception Translator in the spring beans config file :

When using EclipseLink though this doesn't work... So I looked for implementations of the spring interface :

org.springframework.dao.support.PersistenceExceptionTranslator 

And found out that there is not an EclipseLinkExceptionTranslator (as for Hibernate) but there is a EclipseLinkJpaDialect
   org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect

That implements the interface so I created a bean in the spring configuration file and I was able to deploy the app on the glassfish.

OSX show used ports or listening applications with their PID

On OSX you can display applications listening on a given port using the lsof the commands described below will show listening application...