There is an application for spring boot. I want to write a class that will do a reconnect to RethinkDB if the base has fallen. I launched a task that checks if the connection is alive, and if it is closed, then I need to create a new connection and add it to the application context. The class is as follows:

 package service; import com.rethinkdb.RethinkDB; import com.rethinkdb.net.Connection; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service public class RethinkDBReconnector { @Autowired private RethinkDB r; @Autowired private Connection conn; @Scheduled(fixedRate = 1000) public void task() { if (!conn.isOpen()) { System.out.println("closed"); } } } 

The connection is configured as:

 @Configuration public class SpringConfig { @Bean public Connection connection(RethinkDB r) { return r.connection().hostname("127.0.0.1").port(28015).connect(); } @Bean public RethinkDB rethinkDB() { return RethinkDB.r; } } 

But I do not know how to get the application context and add a new connection to it so that it can be used instead of the old one. I do not know if it can be implemented as a dependency. Most likely no. How can I solve this problem?

The problem is not solved. Assigning a new value to a link in the reconnect does not update the connection globally.

I tried to do this:

 @Bean @Scheduled(fixedRate = 1000) public Connection connection(RethinkDB r) { Connection conn = r.connection().connect(); if (conn == null || (!conn.isOpen())) { conn = r.connection().connect(); } return conn; } 

At startup, an exception occurs:

 Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'connection': Only no-arg methods may be annotated with @Scheduled at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.processScheduled(ScheduledAnnotationBeanPostProcessor.java:496) at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$null$1(ScheduledAnnotationBeanPostProcessor.java:359) at java.base/java.lang.Iterable.forEach(Iterable.java:75) at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$postProcessAfterInitialization$2(ScheduledAnnotationBeanPostProcessor.java:359) at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:358) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:434) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1749) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) ... 63 common frames omitted 
  • Why add it again? I understand that the reference to the Connection object that already uses the context has already been obtained using @Autowired. If you need to create a new connection, create it and assign it to the variable conn. The context will refer to it. - Xoxole
  • @Xoxole, thanks. The connection is re-created, but when you write to the database, an error occurs Can't write query because response pump is not running. How to fix? - typemoon
  • Tell your @Scheduled(fixedRate = 1000) on the connection creation method (with checking for null and for !conn.isOpen() ) - Chubatiy
  • @Xoxole, no, it does not work. The connection is not updated for the entire application. - typemoon
  • @Chubatiy, in what class? In a class configuration of beans? - typemoon

1 answer 1

In the SpringConfig class

 ..... @Autowired private Connection conn; @Bean @Scheduled(fixedRate = 1000) public Connection connection() { if (null != conn && conn.isOpen()) { return conn; } return RethinkDB.r.connection().hostname("127.0.0.1").port(28015).connect(); } ..... 
  • So it doesn't work either. Error creating bean with name 'rethinkDB': Requested bean is currently in creation: Is there an unresolvable circular reference? - typemoon
  • corrected a little. Try again (removed the field) - Chubatiy
  • I also cleaned the field. It still does not work, even if you post the initial configuration of the connection and the reinitialization code into different config files. - typemoon