Search code examples
oracle-databasejdbcglassfishdbeaver

Can't connect to external Oracle database from Glassfish pool, but I can from DBeaver


First, I'm sorry if I say something wrong, English isn't my first language. Also, I'm changing the real names for security (and NDA) reasons

Whenever I try to connect from my Java project with Glassfish to the external database with the URL "jdbc:oracle:thin:@//HOST:1521/SID", DBUSER=auser, DBPASSWORD=apass123, it throws

ORA-01017: invalid username/password; logon denied

Here's how I configured the Glassfish pool:

DataSource and Driver

URL, DBUSER and DBPASSWORD

Glassfish's domain.xml

<jdbc-connection-pool datasource-classname="oracle.jdbc.pool.OracleDataSource" name="NamePool" res-type="javax.sql.DataSource">
      <property name="URL" value="jdbc:oracle:thin:@//HOST:1521/SID"></property>
      <property name="DBPASSWORD" value="apass123"></property>
      <property name="DBUSER" value="auser"></property>
</jdbc-connection-pool>
<jdbc-resource pool-name="NamePool" jndi-name="jdbc/jndiORA"></jdbc-resource>

My project's connect.java, throws error at dataSource.getConnection()

import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public static void Connect(String query) throws NamingException {
        InitialContext ctx = new InitialContext();
        DataSource dataSource = (DataSource) ctx.lookup("jdbc/jndiORA");
        try (Connection conn = dataSource.getConnection()) {
            try (Statement stmt = conn.createStatement()) {
                ResultSet rs = stmt.executeQuery(query);
            }
        }
}

But when I try to connect with DBeaver using the same credentials, it DOES work!

DBeaver Oracle Connection Settings

DBeaver connection, shows the schemas and can do queries

Tried some of the answers provided in similar threads, with no positive results

  • Using "auser" and "apass123" instead of auser and apass123
  • Escaping first character with \ (\auser and \apass)
  • Downloaded last version of ojdbc6.jar

I'm not the admin of the external database, so couldn't try the "alter system set sec_case_sensitive_logon" solution

Thank you very much for everything!


Solution

  • The properties should be user and password, not dbuser and dbpassword

    <jdbc-connection-pool datasource-classname="oracle.jdbc.pool.OracleDataSource" name="NamePool" res-type="javax.sql.DataSource">
          <property name="URL" value="jdbc:oracle:thin:@//HOST:1521/SID"></property>
          <property name="USER" value="auser"></property>
          <property name="PASSWORD" value="apass123"></property>
    </jdbc-connection-pool>
    

    Also /SID is actually /service-name, so check that's correct, and that you're attempting to the right CDB/PDB - though from DBeaver screenshots are a bit confusing, as that seems to be specifying a SID in the connection panel but showing a service-name URL in the second image.