springboot app connects to postgres even db server is disconnected - postgresql

I have a springboot app that connects to PostgreDB.
here is the datasource bean
#Bean
public DriverManagerDataSource createDataSourceBean(Environment env) {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5432/postgres");
dataSource.setUsername("postgres");
dataSource.setPassword("<myspassword>");
return dataSource;
}
This works fine, but I wonder why even if I try to disconnect the database server via pgAdmin and then run the app my app still works. I believe I should encounter an exception when dbserver is disconnected.
I'm sure there's something weird happening . . .

Related

MongoDB URI Configuration

I am using MongoDB URI (mongodb://user:pass#IP:27017/myDB?retryWrites=false&connectTimeoutMS=10000) configuration in Spring-boot. I observed approx every 5 min. Mongodb not responding for first hit, Second hit working fine.
Some time getting this message "Opened connection [connectionId{localValue:8}]" in log.
Java Configuration.
#Bean
public MongoDbFactory mongoDbFactory() {
return new SimpleMongoDbFactory(new MongoClientURI(prop.getDbConnectionUri()));
}
#Bean
public MongoTemplate mongoTemplate() {
log.info("Loging MongoDB Config Loging...");
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
return mongoTemplate;
}
Please suggest any other optional configuration required in Mongo URI.
After so many Changes,I fixed this problem by changing below system configuration. This is not related to mongo configuration.
Open sysctl.conf by using "sudo vim /etc/sysctl.conf" command.
Change "net.ipv4.tcp_keepalive_time = 220".
Save Changes.
Run "sysctl --system" command.
And Restart your micro service.
Now Mongo Connection working fine.

Debugging connection PostgreSQL Loopback 4

Im on a mac(OS 10.14) using nodejs 14 and PostgresSQL 12.
I just installed Loopback4 and after following this tutorial Im not able to use any of the enpoints that use Models, ie that connect to Postgres, I constantly get a timeout.
It seems like its not even reaching the Postgres Server, but the error gives no information, just that the request times out.
There are no issues with the Postgres server since I can connect and request information with other nodejs applications to the same database.
I also tried to set this as the host host: '/var/run/postgresql/', same result.
I now tried the approach with a Docker container, setting the datasource files as follows:
import {inject, lifeCycleObserver, LifeCycleObserver} from '#loopback/core';
import {juggler} from '#loopback/repository';
const config = {
name: 'mydb',
connector: 'postgresql',
url: 'postgres://postgres:mysecretpassword#localhost:5434/test',
ssl: false,
};
// Observe application's life cycle to disconnect the datasource when
// application is stopped. This allows the application to be shut down
// gracefully. The `stop()` method is inherited from `juggler.DataSource`.
// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html
#lifeCycleObserver('datasource')
export class PostgresSqlDataSource extends juggler.DataSource
implements LifeCycleObserver {
static dataSourceName = 'PostgresSQL';
static readonly defaultConfig = config;
constructor(
#inject('datasources.config.PostgresSQL', {optional: true})
dsConfig: object = config,
) {
super(dsConfig);
}
}
With that same url I can log on my command line from my mac.
Is there a way to add logging and print any connection error? Other ways to debug it?
[UPDATE]
As of today Loopback4 Postgres connector does not work properly with Nodejs 14.
When starting the application, instead of running
npm start, you can set the debug string by running:
DEBUG=loopback:connector:postgresql npm start
If you want it to be more generic, you can use:
DEBUG=loopback:* npm start

java.sql.SQLException: No suitable driver found for jdbc:mariadb://localhost:3306/mydatabase

I have mariadb 10.4 running on my system. The db connection is good, i have tested with db workbench with the root password.
On the other hand, I try to connect to it from my Java code, i have the exception below:
java.sql.SQLException: No suitable driver found for jdbc:mariadb://localhost:3306/mydatabase
My java code is as below:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class CountryDAO {
public void list() throws SQLException{
String databaseURL = "jdbc:mariadb://localhost:3306/mydatabase";
String user = "root";
String password = "root";
try
{
Connection connection = DriverManager.getConnection(databaseURL, user, password);
} catch (SQLException ex) {
ex.printStackTrace();
throw ex;
}
}
}
I am calling this code from a servlet.
I am using eclipse and i have added the mariadb-java-client-2.4.2.jar as external jar from build path. It should be working but it doesn't.
UPDATE: Firstly, I have tested my code in a new Eclipse Java project and it works like a charm. Secondly, i have tried to call the same method from a main method of another class, the db connection also work perfectly. So i guess my problem is because i call this connection from a Servlet. I don't know why it does so but i am kind of stuck. Do you have any suggestions?
You have to load the driver first.
Class.forName("org.mariadb.jdbc.Driver");

PostgreSQL database Versioning using Liquibase

I'm trying to use Liquibase version 3.6 to maintain database version changes. I'm able to execute database version changes when i need to execute single file changes.
I have using below code to execute version changes successfully, but my intention is to dynamically execute the change logs from a folder and not single file. I'm using only Java for all configuration of Liquibase
#Startup
#Singleton
#TransactionManagement(TransactionManagementType.BEAN)
public class InitializerBean {
#Resource(mappedName = "java:/M2M_RESOURCES")
private DataSource ds;
#PostConstruct
protected void bootstrap() {
ResourceAccessor resourceAccessor = new ClassLoaderResourceAccessor(getClass().getClassLoader());
try (Connection connection = ds.getConnection()) {
JdbcConnection jdbcConnection = new JdbcConnection(connection);
Database db = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(jdbcConnection);
Liquibase liquiBase = new Liquibase("data/liquibase/", resourceAccessor, db);
liquiBase.update("Development");
} catch (SQLException | LiquibaseException e) {
}
}
}
When the first parameter of Liquibase class is single file , liquibase is able to execute changes but when I intent to execute all file of single folder is not able to track and execute changes.
I'm using JDK 1.8 and file in data/liquibase/ is dbChangelog.sql and dbChangelog_2.sq. This all code is deployed on Wildfly 10 as part of ear archive

How to include driver class and/or class path to connect to a SQL Server 2000 database in Jdeveloper?

I want to connect to a database in Jdeveloper 11g but there seems to be a problem with my driver for SQL Server 2000. I have downloaded the appropriate driver (Sqljdbc4.jar) but I do not know how to include or use it in my code.
import java.sql.*;
class TestConnection
{
public static void main (String[] args)
{
try
{
// Step 1: Load the JDBC ODBC driver
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Step 2: Establish the connection to the database
String url = "jdbc:sqlserver://10.1.73.180\\SQL2000;" +
"databaseName=reportgen;user=sa;password=*****;";
Connection conn = DriverManager.getConnection(url);
System.out.println("Connected.");
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
and this shows when I try to run my TestConnection.java class:
Got an exception!
com.microsoft.sqlserver.jdbc.SQLServerDriver
Process exited with exit code 0.
Project properties->libraries and class path->add JAR.
You might also need to add this to the embedded WebLogic instance.
You can also use tools->manage libraries to add a library and then add that library to your project.