Erro de configuração: "java.net.ConnectException: Connection refused"
Autor: Rodolfo Vasconcelos | Publicado: 2012-10-31T04:20:00-07:00

Ás vezes um descuido ao editar um arquivo de configuração pode causar um transtorno enorme, principalmente quando o projeto está em fase de integração e com o cronograma apertado.



Como um exemplo prático, um sistema passou a não conseguir conectar mais com a base de dados de uma hora para outra, sem motivo aparente. A exceção era:




Exception in thread "main" 
org.springframework.transaction.CannotCreateTransactionException: Could not open 
JPA EntityManager for transaction; nested exception is 
javax.persistence.PersistenceException: 
org.hibernate.exception.LockAcquisitionException: Cannot open 
connection

        at 
org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:375)

        at 
org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:374)

        at 
org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:263)

        at 
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:101)

        at 
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)

        at 
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)

        at 
$Proxy18.startBeh(Unknown Source)

        at 
br.com.beh.Main.main(Main.java:58)

Caused by: 
javax.persistence.PersistenceException: 
org.hibernate.exception.LockAcquisitionException: Cannot open 
connection

        at 
org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)

        at 
org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:41)

        at 
org.springframework.orm.jpa.DefaultJpaDialect.beginTransaction(DefaultJpaDialect.java:70)

       at 
org.springframework.orm.jpa.vendor.HibernateJpaDialect.beginTransaction(HibernateJpaDialect.java:52)

        at 
org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:330)

        ... 7 
more

Caused by: 
org.hibernate.exception.LockAcquisitionException: Cannot open 
connection

        at 
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:110)

        at 
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)

        at 
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)

        at 
org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)

        at 
org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)

        at 
org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)

        at 
org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)

        at 
org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1353)

        at 
org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:38)

        ... 10 
more

Caused by: 
java.sql.SQLException: The Network Adapter could not establish the 
connection

        at 
oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)

        at 
oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)

        at 
oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199)

        at 
oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:480)

        at 
oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:413)

        at 
oracle.jdbc.driver.PhysicalConnection.&lt;init&gt;(PhysicalConnection.java:508)

        at 
oracle.jdbc.driver.T4CConnection.&lt;init&gt;(T4CConnection.java:203)

        at 
oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)

        at 
oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:510)

        at 
java.sql.DriverManager.getConnection(DriverManager.java:582)

        at 
java.sql.DriverManager.getConnection(DriverManager.java:154)

        at 
org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:174)

        at 
org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:165)

        at 
org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:149)

        at 
org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:119)

        at 
org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection(InjectedDataSourceConnectionProvider.java:46)

        at 
org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)

        ... 15 
more

Caused by: 
oracle.net.ns.NetException: The Network Adapter could not establish the 
connection

        at 
oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:328)

        at 
oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:421)

        at 
oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:630)

        at 
oracle.net.ns.NSProtocol.connect(NSProtocol.java:206)

        at 
oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:966)

        at 
oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:292)

        ... 27 
more

Caused by: 
java.net.ConnectException: Connection refused

        at 
java.net.PlainSocketImpl.socketConnect(Native Method)

        at 
java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)

        at 
java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)

        at 
java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)

        at 
java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)

        at 
java.net.Socket.connect(Socket.java:529)

        at 
java.net.Socket.connect(Socket.java:478)

        at 
java.net.Socket.&lt;init&gt;(Socket.java:375)

        at 
java.net.Socket.&lt;init&gt;(Socket.java:189)

        at 
oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:127)

        at 
oracle.net.nt.ConnOption.connect(ConnOption.java:126)

        at 
oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:306)

        ... 32 
more




Depois de várias horas debugando, descobrimos que esse erro foi causado por um erro na edição do database.properties, na string de conexão com a base.



A propriedade estava assim:

database.url = jdbc:oracle:thin:&lt;IP&gt;:1521:BASEDEDADOS



Para uma mente apressada e estressada é fácil não perceber que faltou um arroba (@) antes do IP. O correto é:

database.url = jdbc:oracle:thin:@&lt;IP&gt;:1521:BASEDEDADOS