Issue connecting to Heroku PostgreSQL from local GlassFish - java

I am trying to get a JDBC connection pool and resource setup in a local GlassFish server to connect to a Heroku PostgreSQL instance.
I have tried it in Tomcat setup as a resource and it works ok, but in GlassFish I have had some issues.
First up I had to downgrade my version of Java 8 to 151 to stop one error I was getting. Now when I try and execute the query in my app I get:
java.net.ConnectException: Connection refused (Connection refused)
I think this is related to connections to Heroku PostgreSQL having to use SSL, but I am not sure what I need to do in GlassFish to configure that.
Does anyone have any experience doing this?

After a lot of head scratching and Googling, I finally got this working. The exception above is a red herring. It is complaining it cannot connect to a local Derby database which is the GlassFish default resource. This means the problem was actually related to not being able to find the resource I specified.
Here are the steps I needed to get the connection working in GlassFish 5:
Set JAVA_HOME to be JDK 1.8.0_151 (any newer version of 1.8 does not work) GlassFish needs to run on this version.
In your Bean class use #Resource(mappedName = "jdbc/postgresql"). This was the only way I could get it to find my jdbc/postgresql resource (#Resource(name = "jdbc/postgresql"), #Resource(name = "java:comp/env/jdbc/postgresql") and #Resource(lookup = "java:comp/env/jdbc/postgresql") did not work).
The connection Url must be like this:jdbc:postgresql://<host>:<port>/<databse>?sslmode=require (originally i had this jdbc:postgresql://<host>:<port>/<databse>?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory which had been working for me in other places).
So the key points are to use JDK 1.8.0_151, mappedName in the resource attribute and sslmode=require in the connection string.
Life should not be this complicated...

Related

Is there anyway to connect to Azure SQL Server using jTDS with TLS 1.2?

Our Java app use jTDS 1.3.1 to connect to SQL Server, which works fine until a client trying to put the database on Azure SQL. The connection failed with the following exception:
java.sql.SQLException: Reason: Login failed due to client TLS version being less than minimal TLS version allowed by the server.
I checked the documentation of jTDS, can't see anyway to specify TLS version. Besides replacing it with MS JDBC driver, has anyone succeeded with other work around?
I don't have access to Azure SQL, so I cannot test if reducing the TLS version to 1.0 would work.
Had this same issue when trying to connect a .Net application to a Azure SQL database in Azure managed instance.
Here's how I solved it.
My previous JDBC URL was this:
jdbc:jtds:sqlserver://my-example-instance.c656df8582985.database.windows.net:1433
I just added ssl=require to the end of the URL, so it became:
jdbc:jtds:sqlserver://my-example-instance.c656df8582985.database.windows.net:1433;ssl=require
Solved the issue by chance. Just need to append ";ssl=request" to the connection string. It looks like by default SSL/TLS is disabled. The error message about TLS version is misleading. Hope this help someone in the future.

Glassfish - No suitable driver for mysql when trying to connect with datasource

I am trying to complete this tutorial:
https://netbeans.org/kb/docs/javaee/ecommerce/connect-db.html
Part of it is setting up a database and trying to establish a connection using datasource and connection pooling.
I did everything that is in the tutorial but when i try to run my code i get the following error:
javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver found for jdbc/affablebean"
Here is the code that throws it:
<sql:query var = "result" dataSource = "jdbc/affablebean">
SELECT * FROM category, product
WHERE category.id = product.category_id
</sql:query>
I tried to connect to the database without using connection pooling and datasource and it worked like a charm.
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver registered");
}
catch (ClassNotFoundException ex) {
Logger.getLogger(SqlService.class.getName()).log(Level.SEVERE, null, ex);
}
That means that the driver is in the right lib folder in the glassfish directory so the problem must be somewhere else.
The tutorial has a troubleshooting section where they describe that if i get this kind of error "No suitable driver found for jdbc/affablebean" that means that i do not have resource reference in my web.xml. Well... I DO HAVE ONE and here it is:
<resource-ref>
<description>Connects to database for AffableBean application</description>
<res-ref-name>jdbc/affablebean</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
I have been trying to solve this problem 16 hours a day for more than two day but still NO luck.
Do you guys have some idea what is wrong?
I browsed all the google and stackoverflow and found similar problems but the solution they give is "Make sure the mysql driver is in the right folder on the server".
Why do i connect to the database without dataSource object but can not connect with one?
So i found the solution to the exact same problem I was having.
So it appears that the issue was within web.xml not containing a reference to the datasource.
Simply add,
<resource-ref>
<description>AffableBean DataSource</description>
<res-ref-name>jdbc/affablebean</res-ref-name>
<res-type>javax.sql.ConnectionPoolDataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
to your web.xml file and save and run file(right click inside the file and press "Run File").
Afterwards you should see the database tab pop up in your browser of choice.
I had same error,
the problem is on when you set resource-type:
Resource Type: javax.sql.ConnectionPoolDataSource
try using:
Resource Type:=javax.sql.DataSource
thats strange in tutorial text is (the problem):
Click Next. In Step 5, Add Connection Pool Properties, specify the
following details: Datasource Classname:
com.mysql.jdbc.jdbc2.optional.MysqlDataSource Resource Type:
javax.sql.ConnectionPoolDataSource Description: (Optional) Connects to
the affablebean database
but on screenshot is correct.
I had several issues with this
https://netbeans.org/kb/docs/javaee/ecommerce/connect-db.html
part of ecommerce tutorial.
First problem (no suitable driver fourd) appeared due to improper jdbc resource creation -
i used 'new file \ glassfish \ jdbc connection pool'
instead of 'new file \ glassfish \ jdbc resource'.
Redoing this step registered driver correctly and created pool
(was indicated in glassfish servel log that mysql driver did register on server instance).
After that i couldn't get data via testDataSource.jsp - error was org.apache.derby.client.am.SqlException: Table/View 'CATEGORY' does not exist.
I couldn't figure out why org.apache.derby.client was even mentioned and
after some lookup found discussion on netbeans forum:
https://forums.netbeans.org/ntopic61746.html
This part of discussion was solution for my case:
try to change in step setup file sun-resources.xml or glassfish-resources.xml
Setting up a JDBC data source and connection pool
at step 6 -> change Resource Type to javax.sql.ConnectionPoolDataSource
and change in step setup file web.xml
Referencing the data source from the application
at step 4 -> change Resource Type to javax.sql.ConnectionPoolDataSource
p.s. i'm using netbeans 8.0.2, glassfish 4.1 and jdk1.7.0_21.
There was no javax.sql.ConnectionPoolDataSource option in Netbeans'
interface for 'edit resource reference \ resource type' combobox for web.xml.
So i put this value there manually.
After a very tiresome research, having checked servers tomcat, wildfly,and Jboss, none seemed to work,I had problem with registering new datasource with glassfish, adding new jdbc connectionpool would throw java.lang.runtimeexception, the workaround for this issue was to reconfigure DerbyPool under jdbcconnection pools in admin console and feeding it with required Datasource Classname, url,username,and password to point it the database on mysql server.and it did work.
I think you did not add Mysql JDBC Driver in your project libraries.
Add manually Mysql JDBC Driver in your project libraries and try again.
I think it will work.
I have the exactly problem, and even thru Netbeans IDE, the DB connection test is fine while in Glassfish 4, it is not working, try all possible like include lib in project, put here and there. Finally the problem is fixed by remove Glassfish and install Glassfish 3.1.2.2, the project files is exactly same and working fine. Guess this is a bug or specail setting may needed in Glassfish 4.
I had the same issue. The last 4 hours I was looking for a solution and it was so easy for me...
Make sure the "Status" in JDBC Resource (at Glassfish Admin Console) is Enabled (checkbox is checked) :-(
Same problem here. I spent hours with this, tried some of the solutions offered here, and only one got me a step further: changing the Resource Type from "javax.sql.ConnectionPoolDataSource" to "javax.sql.DataSource.
I immediately stumbled upon the next problem: the table "category" didn't exist. It actually does exist.
I gave up on glassfish (this was my first time trying to use glassfish) and went back to Tomcat. Succes! So I decided it might be helpful to point people to another possible solution that worked for me.
For those who are interested in the Tomcat solution, visit this page http://tomcat.apache.org/tomcat-8.0-doc/jndi-datasource-examples-howto.html#MySQL_DBCP_Example. Very clear explanation with an example. And don't forget to add the JSTL library to your project.
Good luck.

GlassFish Server does not connect to SQL database (error w.r.t. SQL driver)

I am using a brand new developing pc and need to test a personal application that runs on a local GlassFish server 3.1.2 and should connect with a local SQL database called 'funkOneDB' (my IDE is NetBeans 7.2.1). But I can't get the GlassFish server to connect with the database, and the problem seems to be related to the (place of the) SQL driver in the GlassFish Server's directories (more problem specifics in a few lines).
I am fairly certain I correctly set up the related JDBC Resource and Connection Pool on the GlassFish Server (as I mimic a set-up already existing and working properly on another developing pc).
The Resource specifics are:
jndi name: jdbc/FunkResource
pool name: FunkPool
The (most important) Pool specifics are:
pool name: FunkPool
resource type: javax.sql.Datasource
datasource classname: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
additional properties correspond to the specifics in the XML GlassFish-resources of the application (username, password, url, etc.; no problems there)
I first placed the necessary SQL driver in the GlassFish Server's directories, i.e. the file mysql-connector-java-5.1.18-bin.jar at ..\GlassFish3\GlassFish\domains\domain1\lib\ext.
Yet, when I perform a ping test from the JDBC Pool 'FunkPool' at the GlassFish server, I get the following error:
Ping Connection Pool failed for FunkPool. WEB9031: WebappClassLoader unable to load resource [com.mysql.jdbc.SQLError], because it has not yet been started, or was already stopped Please check the server.log for more details.
In the server.log I only find the following extra logging exception and failure info:
(i) Exception while creating an unpooled [test] connection for pool [ FunkPool ], WEB9031: WebappClassLoader unable to load resource [com.mysql.jdbc.SQLError], because it has not yet been started, or was already stopped
(ii) RestResponse.getResponse() gives FAILURE. endpoint = 'http://localhost:4848/management/domain/resources/ping-connection-pool.json'; attrs = '{id=FunkPool}'
Note however, that when I ping the database funkOneDB from my IDE NetBeans via jdbc:mysql://localhost:33066/funkOneDB, it's succesful. As already mentioned, the credentials and other data I use for this IDE-based ping are the same data I use in the JDBC Connection Pool.
I searched for the problem also on stackoverflow for some. And I did find some people talking about it, like
Glassfisch MySQL ping ERROR (no answer by anybody), or
Struggling to create MySQL Connection Pool on Glassfish (tried that solution, i.e. putting the SQL driver one level up in ..\GlassFish3\GlassFish\domains\domain1\lib\, but this creates other errors, even after restarting the Glassfish server), or
GlassFish not loading connector
(even tried this solution, no succes).
Can somebody help me solve this problem? Many thanks in advance!
With kind regards,
Heinz
Place the mysql driver in the lib folder of your project. Then do a clean-and-build. It's also helpful to have netbeans communicate directly with your database. This will allow you to view the database structure and the contents of your database right from your IDE. For help integrating MySQL with netbeans, look here: netbeans.org/kb/docs/ide/mysql.html
My friend, i had this same exception:
RestResponse.getResponse() gives FAILURE. endpoint = 'http://localhost:4848/management/domain/resources/ping-connection-pool.json'; attrs = '{id=FunkPool}'
The cause of my error was that, i put wrong credentials. Check your credentials in your client DB App (SQL Developer, for example).
I had the same problem with SQL server and Netbeans. To resolve it, i put the sqljdbc.jar in the java direcory "Java\jdk1.8.0_121\lib directory" and it works :)
I've just spebnt 10 hours on this bug.

Connection JDBC SQL Server Error

I've tryed to create a connection with a Microsoft SQl Server 2008 database through JDBC on Eclipse SDK. I've dowloaded JDBC driver from microsoft and I've installed it, then I've added at my System environment variables CLASSPATH the path of sqljdbc4.jar file. After icluding in the Eclipse project the jar file I've tryed to create the connection using:
String connectionUrl = "jdbc:sqlserver://localhost;integratedSecurity=true";
Connection con = DriverManager.getConnection(connectionUrl);
but it doesn't works, and launch me this exception:
com.microsoft.sqlserver.jdbc.SQLServerException: TCP/IP connection
at host localhost, port 1433 failed. Error: "Connection refused:
connect. Verify connection properties and make sure an instance of SQL
Server is running on the host and is accepting TCP/IP connections at
the port. Be sure no firewall blocks connections at the port.
I'm working on a JRE 1.6 so a sqljdbc4 should work, and I've created a working ODBC, so the server is responding, and the error should be in java command or JDBC installation.
Can anyone help me?
At the risk of stating the obvious, this looks to me as if TCP connections haven't been enabled on SQL Server. You have to manually enable them, they don't come enabled by default.
There's an article on MSDN here which explains how to enable TCP protocols for SQL 2005/2008.
Following are the quick trouble shooters:
Try to connect to your server using external front end.
Check if your firewall blocks the connection to the port
Check to see if server is really up.
A Suggestion :
If you are using eclipse , you don't need to add the jar into CLASSPATH variable , you can just add it in library of your project to make it available at runtime

Error while connecting to DB2 from JBoss Application Server

I'm trying to connect to the AS400 database DB2 from a Java application hosted in JBoss application server. But, I'm getting the below error when ever I run my application:
Apparently wrong driver class specified for URL: class: com.ibm.as400.access.AS400JDBCDriver, url: jdbc:as400://DBSYTEM;driver=toolbox;trace=false;errors=full;prefetch=true;naming=system;libraries=*LIBL
Has anyone ever faced similar problem before?
Thanks,
Veera.
I had exactly the same problem.
Setting connection-url as the following resolved the problem.
jdbc:as400://[systemname]/[schema];extended dynamic=true;package=jbpkg;package cache=true;package library=jboss;errors=full
From the connection URL in your question, I think you're using DB2 on AS/400.
Have you verified the existence of the correct classpath of IBM DB2 for AS/400 JDBC driver? The JDBC driver of IBM DB2 for AS/400 is not provided by JBoss Application Server, it's provided only from IBM.

Categories