Refresh database configuration on the fly - java

I have database configuration in the properties file:
port=8080
host=host-default
host-default is obviously DNS. Below is my configuration class:
#Configuration
#Slf4j
public class DatabaseConfig {
#Value("${port}")
private int port;
#Value("${host}")
private String hostname;
#Bean
public DatabaseTemplate databaseTemplate() {
try {
return new DatabaseTemplate(client());
} catch (Exception e) {
log.error("Ex: " + e.getMessage(), e);
return null;
}
}
#Bean
public Client client() throws UnknownHostException {
TransportAddress address = new InetSocketTransportAddress(InetAddress.getByName(this.hostname), this.port);
client.addTransportAddress(address);
return client;
}
}
So, there is a problem. When the server is running, and in meantime I change DNS the connection with DB will fall dawn. At this moment I cant refresh configuration. I can catch moment when DNS change but I cannot update config. Have you any idea? I tried to destroy DatabaseTemplate singleton but It does not help. Thanks

You will need to create a new bean that wraps the database connection, then update it based on a schedule :
#Component
public class DataSourceManager implements DataSource{
private DataSource dataSource;
#PostConstruct
#Scheduled(fixedRate=1000)
public void reload() {
// init the datasource
}
public DataSource getDataSource(String dbName) {
return dataSource;
}
#Override
public Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
.... wrap all the other DataSource methods
}

Related

How to see and add to logs in libraries which are referenced in java spring boot project?

I have a spring boot application that uses the libraries: SimpleMessageListenerContainer (https://docs.spring.io/spring-amqp/docs/current/api/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainer.html) and SimpleMessageListenerContainerFactory (https://www.javadoc.io/static/org.springframework.cloud/spring-cloud-aws-messaging/2.2.0.RELEASE/org/springframework/cloud/aws/messaging/config/SimpleMessageListenerContainerFactory.html). The application uses ASW SQS and Kafka but I'm experiencing some out of order data and trying to investigate why. Is there a way to view logging from the libraries? I know I cannot edit them directly but when I create the bean, I want to be able to see the logs from those two libraries and if possible to add to them.
Currently I'm setting up the bean in this way:
#ConditionalOnProperty(value = "application.listener-mode", havingValue = "SQS")
#Component
public class SqsConsumer {
private final static Logger logger = LoggerFactory.getLogger(SqsConsumer.class);
#Autowired
private ConsumerMessageHandler consumerMessageHandler;
#Autowired
private KafkaProducer producer;
#PostConstruct
public void init() {
logger.info("Loading SQS Listener Bean");
}
#SqsListener("${application.aws-iot.sqs-url}")
public void receiveMessage(String message) {
byte[] decodedValue = Base64.getDecoder().decode(message);
consumerMessageHandler.handle(decodedValue, message);
}
#Bean
public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory(AmazonSQSAsync amazonSqs) {
SimpleMessageListenerContainerFactory factory = new SimpleMessageListenerContainerFactory();
factory.setAmazonSqs(amazonSqs);
factory.setMaxNumberOfMessages(10);
factory.setWaitTimeOut(20);
logger.info("Created simpleMessageListenerContainerFactory");
logger.info(factory.toString());
return factory;
}
}
For reference, this is a method in the SimpleMessageListenerContainer. It is these logs which I would like to investigate and potentially add to:
#Override
public void run() {
while (isQueueRunning()) {
try {
ReceiveMessageResult receiveMessageResult = getAmazonSqs()
.receiveMessage(
this.queueAttributes.getReceiveMessageRequest());
CountDownLatch messageBatchLatch = new CountDownLatch(
receiveMessageResult.getMessages().size());
for (Message message : receiveMessageResult.getMessages()) {
if (isQueueRunning()) {
MessageExecutor messageExecutor = new MessageExecutor(
this.logicalQueueName, message, this.queueAttributes);
getTaskExecutor().execute(new SignalExecutingRunnable(
messageBatchLatch, messageExecutor));
}
else {
messageBatchLatch.countDown();
}
}
try {
messageBatchLatch.await();
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
catch (Exception e) {
getLogger().warn(
"An Exception occurred while polling queue '{}'. The failing operation will be "
+ "retried in {} milliseconds",
this.logicalQueueName, getBackOffTime(), e);
try {
// noinspection BusyWait
Thread.sleep(getBackOffTime());
}
catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
}
SimpleMessageListenerContainer.this.scheduledFutureByQueue
.remove(this.logicalQueueName);
}
How would I be able to see all of that logging from where I create the bean?
Any help would be much appreciated!

"No operations allowed after connection closed " using Dart and Java

I am working with Spring Boot and Dart. When I hit the URL (using POSTMEN/Browser) to insert some data in MySql I got the response correctly. But WHen I send the 3 requests consecutively from Flutter Front-end using Dart language it most of the time returned the result of 2 GET request and through the error for the 3rd request and most of the time it works for all request.
Following is the connection service that I am using on backend to store the data.
ConnectionService.java
#Service
public class ConnectionService {
private static final Logger log = LoggerFactory.getLogger(ConnectionService.class);
Connection connection = null;
#Value("${spring.datasource.url}")
String datasourceUrl = "jdbc:mysql://localhost/databaseName?autoReconnect=true&useSSL=false";
public Connection createConnection() throws SQLException {
connection = DriverManager.getConnection(datasourceUrl, "root", "root");
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
return connection;
}
public void closeConnection() {
try {
connection.close();
} catch (Exception e) {
log.error(e.toString());
}
}
}
I am creating the object of the Connection service class and call the createConnection() to create the connection and closeConnection() to close that one.
Controller.java
#RestController
#RequestMapping("/user")
public class UserController {
#Autowired
ConnectionService connectionService;
#GetMapping(path = "/test")
public void testFunction(#RequestParam(name = "abc") String abc) throws SQLException
{
Connection connection = connectionService.createConnection();
if (abc.isExist(param1,param2,connection))
{
//some code
connectionService.closeConnection();
} else
{
//some operation
connectionService.closeConnection();
}
}
Guide me to resolve this issue.
Thanks
They problem is that instead of closing the instance connection that I have created locally I am closing the main connectionService instance. This is the problem.
Now connectionService.closeConnection();
It should be connection.closeConnection();

Trying to Understand Java Dependency Injection with Example

I'm trying to create a database connection library to be used in all my apps. I want to make sure that this library is fully unit tested and so i'm trying to use dependency injection.
I have this class which i want to ensure is tested:
public class ConnectionFactory {
private String dataSourceName;
public ConnectionFactory(String dataSourceName) {
if(dataSourceName == null) {
throw new NullPointerException("dataSourceName can't be null");
}
this.dataSourceName = dataSourceName;
}
public Connection getConnection() throws SQLException {
Connection connection = getDataSource(dataSourceName).getConnection();
if(connection != null) {
return connection;
}
...
}
// Get a datasource object
private DataSource getDataSource(String dataSourceName) {
...
try {
Context ctx = new InitialContext();
dataSource = (DataSource) ctx.lookup("java:comp/env/" + dataSourceName);
} catch (NamingException e) {
...
}
return dataSource;
}
}
I want to be able to simply call this class from all my apps with something as simple as this:
public class MyApp {
public static void main(string[] args) {
ConnectionFactory connectionFactory = new ConnectionFactory("jdbc/myDataSource");
Connection connection = connectionFactory.getConnection();
}
}
I've started writing unit tests for this ConnectionFactory, but quickly realized that with my current code I can't mock the DataSource object so it's trying to actually connect to a real data source.
#RunWith(Nested.class)
public class ConnectionFactoryTest {
public class Constructor {
#Test
public void shouldThrowNullPointerIfParamIsNull() {
assertThatExceptionOfType(NullPointerException.class)
.isThrownBy(() -> { new ConnectionFactory(null); })
.withMessage("dataSourceName can't be null");
}
}
public class GetConnection {
public class WithDataSourceAvailable {
#Test
public void shouldErrorIfParamIsNull() {
ConnectionFactory connectionFactory = new ConnectionFactory("jdbc/myDataSource"); // <-- This is going to fail b/c it's trying to actually fetch a real data source
}
}
}
}
How can I properly use Dependency Injection so that I can write unit tests that don't actually try to connect to a data source?
Take a look at Mockito I find it easy to use for this type of mocking.

How do I test the methods of with Mockito/Powermockito

I have a class that needs to be unit tested:
public class AMQProducer {
private final String TCP = "tcp://";
private final String COLON = ":";
AMQProducer() {
}
public AMQProducer(String ip, long port) throws JMSException {
try {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(TCP + ip + COLON + port);
Connection connection = connectionFactory.createConnection();
} catch (JMSException e) {
throw e;
}
}
}
I have a test case:
#RunWith(PowerMockRunner.class)
#PrepareForTest()
public class TestAMQProducer {
#Mock
Connection connection;
#Mock
ActiveMQConnectionFactory connectionFactory;
#Test
public void test() throws Exception {
PowerMockito.whenNew(ActiveMQConnectionFactory.class).withAnyArguments().thenReturn(connectionFactory);
PowerMockito.when(connectionFactory.createConnection()).thenReturn(connection);
AMQProducer producer = new AMQProducer("random", 1234);
}
}
When I run the test case, the mock object of ActiveMQConnectionFactory isn't being used. Instead, the actual implementation is being used and there's a TCP connection being made:
javax.jms.JMSException: Could not connect to broker URL: tcp://random:2333. Reason: java.net.UnknownHostException: random
I tried with Powermockito and Mockito, but failed with both. How do I mock the objects and how do I successfully run the test case?
I am new to unit testing and tried to get help from various communities, but didn't find the appropriate answer. Any help would be appreciated. Thanks!
You cannot test it with the current structure of your code - you create new ActiveMQConnectionFactory in your constructor. Use dependency injection instead.
With Mockito:
public class AMQProducer {
AMQProducer() {}
public AMQProducer(ActiveMQConnectionFactory connectionFactory) throws JMSException {
Connection connection = connectionFactory.createConnection();
}
}
public class TestAMQProducer {
private final Connection connection = mock(Connection.class);
private final ActiveMQConnectionFactory connectionFactory = mock(ActiveMQConnectionFactory.class);
#Test
public void test() throws Exception {
doReturn(connection).when(connectionFactory).createConnection();
// actual test here
}
}

Connection pool with HikariCP to multiple databases

I am developing a monitoring plugin that queries multiple database. I would like to use HikariCP in order to keep the connection open, but I do not know how the connection pool has to be instantiated.
Does HikariCP use just one pool for several databases? or just one pool for one database, and it is my responsibility to instantiate as many pools as database I will use.
The latter: a pool is associated to a single database configuration parameters, and it is your responsibility to instantiate as many pools as database I will use. Create the pools accordingly.
I have a DataSourceFactory to accomplish this:
public final class DataSourceFactory {
private static final Logger LOG = LoggerFactory.getLogger(DataSourceFactory.class);
//connection to MySQL
private static DataSource mySQLDataSource;
//connection to PostgreSQL
private static DataSource postgresDataSource;
private DataSourceFactory() { }
//generic method to create the DataSource based on configuration
private static DataSource getDataSource(String configurationProperties) {
Properties conf = new Properties();
try {
conf.load(DataSourceFactory.class.getClassLoader().getResourceAsStream(configurationProperties));
} catch (IOException e) {
LOG.error("Can't locate database configuration", e);
}
HikariConfig config = new HikariConfig(conf);
HikariDataSource dataSource = new HikariDataSource(config);
return dataSource;
}
//retrieve the datasource for MySQL
public static DataSource getMySQLDataSource() {
LOG.debug("Retrieving data source for MySQL");
if (mySQLDataSource == null) {
synchronized(DataSourceFactory.class) {
if (mySQLDataSource == null) {
LOG.debug("Creating data source for MySQL");
mySQLDataSource = getDataSource("mysql-connection.properties");
}
}
}
return mySQLDataSource;
}
//retrieve the datasource for Postgres
public static DataSource getPostgresDataSource() {
LOG.debug("Retrieving data source for Postgres");
if (postgresDataSource == null) {
synchronized(DataSourceFactory.class) {
if (postgresDataSource == null) {
LOG.debug("Creating data source for Postgres");
postgresDataSource = getDataSource("postgres-connection.properties");
}
}
}
return postgresDataSource;
}
}
Here's a file configuration example:
dataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
dataSource.url=jdbc:mysql://theHostName:thePort/nameOfDatabase
dataSource.user=user
dataSource.password=thIsIsN07mYR3alPa$s
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=100
dataSource.prepStmtCacheSqlLimit=2048
dataSource.useServerPrepStmts=true
autoCommit=false
maximumPoolSize=10

Categories

Resources