0

The problem is simple when I use Spring boot 1.5.2 with Hibernate to connect to a datasource with configued like this:

@Bean
public DataSource dataSource() {
   DataSourceBuilder = datasourceBuilder = DataSourceBuilder.create();
   dataSourceBuilder.url(DATASOURCE_URL);
   dataSourceBuilder.username(DATASOURCE_USERNAME);
   dataSourceBuilder.password(DATASOURCE_PASSWORD);
   return dataSourceBuilder.build();
}

and this is a Command line application, so when the application is almost done, I just want to close the Hibernate connections to the database and rename the database with JDBC. However, I don't know how to do this in Spring boot, any idea?

I tried to inject the DataSource object to a class to rename database but it cannot close the connections to database.

@Autowired
private DataSource dataSource;

public void closeConnection() {
    dataSource.close();
}

with error

org.postgresql.util.PSQLException: ERROR: database "DATABASE" is being accessed by other users
Detail: There are 10 other sessions using the database.
Bằng Rikimaru
  • 1,512
  • 2
  • 24
  • 50

1 Answers1

1

You can use pg_terminate_backend() to kill a connection. You have to be superuser to use this function. This works on all operating systems the same.

refer this stackoverflow link :

https://stackoverflow.com/a/5109190/7801800

Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17