In production we are connecting with a MongoDB using Java (connection pooling). Every day we are getting almost 500 requests with the error below (MongoSocketReadTimeoutException) and we are not using any complex query. Could it be stale Mongo connections?
I didn't see a problem with our code or MongoDB slowness. Please review my code below and suggest if any parameters need to be added or anything needs to be changed.
at 2018-03-07 19:52:43 ERROR ::Error while connecting the Mongo DB {}
com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message
at com.mongodb.connection.InternalStreamConnection.translateReadException(InternalStreamConnection.java:474)
at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:225)
at com.mongodb.connection.UsageTrackingInternalConnection.receiveMessage(UsageTrackingInternalConnection.java:102)
at com.mongodb.connection.DefaultConnectionPool$PooledConnection.receiveMessage(DefaultConnectionPool.java:435)
at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:112)
at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159)
at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:286)
at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:173)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:215)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:206)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:112)
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:487)
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:482)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:239)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:212)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:482)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:79)
at com.mongodb.Mongo.execute(Mongo.java:772)
at com.mongodb.Mongo$2.execute(Mongo.java:759)
at com.mongodb.FindIterableImpl$FindOperationIterable.first(FindIterableImpl.java:207)
at com.mongodb.FindIterableImpl.first(FindIterableImpl.java:148)
at com.tecnotree.bom.validation.dao.MongoManager.getJsonObject(MongoManager.java:88)
at com.tecnotree.bom.validation.dao.CustomerMasterDao.getService(CustomerMasterDao.java:48)
at com.tecnotree.bom.validation.service.ValidationService.processValidation(ValidationService.java:214)
at com.tecnotree.bom.validation.service.ValidationService.processRequest(ValidationService.java:125)
at sun.reflect.GeneratedMethodAccessor185.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvid
er.java:205)
Code
public class ConnectionManager {
private ConnectionManager() {
}
private static final Logger logger = LoggerFactory.getLogger(ConnectionManager.class);
private static MongoClient mongoClient = null;
private static String connections = null;
private static String connectionTimeOut = null;
private static String socketTimeOut = null;
private static String serverSelectionTimeOut = null;
private static String URL = null;
private static String username = null;
private static String password = null;
private static String hostname = null;
private static String port = null;
private static String maxConnectionIdleTime = null;
private static String minConnectionsPerHost = null;
private static String maxConnectionLifeTime = null;
private static String dbName = null;
static {
dbName = ValidationProperties.getValue("clm.db.dbName");
mongoClient = getCLMSecondaryMongoConnection();
}
private static MongoClient getCLMSecondaryMongoConnection() {
connections = ValidationProperties.getValue("clm.db.connectionsPerHost");
connectionTimeOut = ValidationProperties.getValue("clm.db.connectTimeoutMS");
socketTimeOut = ValidationProperties.getValue("clm.db.socketTimeOut");
serverSelectionTimeOut = ValidationProperties.getValue("clm.db.serverSelectionTimeOut");
username = ValidationProperties.getValue("clm.db.userName");
password = ValidationProperties.getValue("clm.db.password");
hostname = ValidationProperties.getValue("clm.db.hostname");
port = ValidationProperties.getValue("clm.db.portnumber");
maxConnectionIdleTime = ValidationProperties.getValue("clm.db.maxConnectionIdleTime");
minConnectionsPerHost = ValidationProperties.getValue("clm.db.minConnectionsPerHost");
try {
List<MongoCredential> creds = new ArrayList<MongoCredential>();
creds.add(MongoCredential.createCredential(username, dbName, password.toCharArray()));
/*creds.add(MongoCredential.createMongoCRCredential(username, dbName, password.toCharArray()));*/
MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder();
optionsBuilder.connectTimeout(Integer.parseInt(connectionTimeOut));
optionsBuilder.serverSelectionTimeout(Integer.parseInt(serverSelectionTimeOut));
optionsBuilder.socketTimeout(Integer.parseInt(socketTimeOut));
optionsBuilder.connectionsPerHost(Integer.parseInt(connections));
optionsBuilder.maxConnectionIdleTime(Integer.parseInt(maxConnectionIdleTime));
optionsBuilder.minConnectionsPerHost(Integer.parseInt(minConnectionsPerHost));
if(Boolean.valueOf(ValidationProperties.getValue("clm.db.useSecondaryMongoForRead"))) {
logger.info("Read is going for secondary mongoDB");
optionsBuilder.readPreference(ReadPreference.secondaryPreferred());
}
MongoClientOptions options = optionsBuilder.build();
mongoClient = new MongoClient(new ServerAddress(hostname, Integer.parseInt(port)), creds, options);
} catch (Exception e) {
logger.error("Error while connecting the Mongo DB {}", e);
}
return mongoClient;
}
public static MongoClient getMongoClient() {
return mongoClient;
}
In your code you are setting various MongoClientOptions, but you do not say what values you are using. Most likely one of the settings is causing the problem. My first guess would be that socketTimeout is too small, my second guess would be that connectionsPerHost is smaller than the maximum number of concurrent requests you receive in production.
I assume that testing in your production environment is not an option, so the first thing you should do is try and reproduce the problem in a test environment. You can use the free JMeter tool to reproduce a certain load to your server. Use it's ramp-up feature to see if your problems start once you reach a certain load or if they occur at random.
If you still have trouble finding out what exactly causes the problems, you can also try to comment out all your optionBuilder settings (so you'll be using all default settings). If that fixes the problem, then you can set back your configuration options one-at-a-time to see which is the problematic one.
You must remove hibernate dependency from your project.
I faced this problem and that was the solution.
Related
I am dealing with high traffic in my Spring Boot project and my goal is serving clients as much fast as possible. In this case, I have more than 500 requests per second. In each rest endpoint call, I should connect my schema and gather multiple information from multiple tables. To be able to do that, should I create new connection for each eendpoint call or create & close before each db query?
I wrote a JDBC connection class but I am not sure that it is a good way. Maybe you can give me some opinion.
JDBC Connection Class
#PropertySource({"classpath:application.properties"})
#Configuration
public class FraudJDBConfiguration {
private final Logger LOGGER = LogManager.getLogger(FraudJDBConfiguration.class);
private final Environment env;
#Autowired
public FraudJDBConfiguration(Environment env) {
this.env = env;
}
#Bean
public Connection getFraudConnection() {
// Step 1: Loading or
// registering Oracle JDBC driver class
String connectionClass = env.getProperty("fraud.db.driver-class-name");
try {
Class.forName(connectionClass);
} catch (ClassNotFoundException cnfex) {
LOGGER.error(cnfex.getMessage());
throw new RuntimeException("JDBC driver class'ı bulunamadı");
}
// Step 2: Opening database connection
try {
String environmentType = env.getProperty("environment");
if (environmentType == null) {
LOGGER.error("environment Tip Hatası (TEST - UAT - LIVE)");
throw new RuntimeException("environment Tip Hatası (TEST - UAT - LIVE)");
} else {
String connectionString = null;
String username = null;
String password = null;
switch (environmentType.toLowerCase()) {
case "dev":
connectionString = env.getProperty(/*someurl*/);
username = env.getProperty(/*someusername*/);
password = env.getProperty(/*somepassword*/);
break;
case "tst":
connectionString = env.getProperty(/*someurl*/);
username = env.getProperty(/*someusername*/);
password = env.getProperty(/*somepassword*/);
break;
case "liv":
connectionString = env.getProperty(/*someurl*/);
username = env.getProperty(/*someusername*/);
password = env.getProperty(/*somepassword*/);
break;
case "uat":
connectionString = env.getProperty(/*someurl*/);
username = env.getProperty(/*someusername*/);
password = env.getProperty(/*somepassword*/);
break;
}
// Step 2.A: Create and
// get connection using DriverManager class
if (connectionString == null) {
LOGGER.error("fraud şeması için connection string bulunamadı");
throw new RuntimeException("fraud şeması için connection string bulunamadı");
}
return DriverManager.getConnection(connectionString, username, password);
}
} catch (SQLException e) {
LOGGER.error(e.getMessage());
}
return null;
}
}
DAO
#Component
public interface FraudCommTransactionsDao {
Long count();
}
DAO IMPL
#Service
public class FraudCommTransactionsDaoImpl implements FraudCommTransactionsDao {
private final FraudJDBConfiguration fraudJDBConfiguration;
#Autowired
public FraudCommTransactionsDaoImpl(FraudJDBConfiguration fraudJDBConfiguration) {
this.fraudJDBConfiguration = fraudJDBConfiguration;
}
#Override
public Long count() {
try(Connection connection = fraudJDBConfiguration.getFraudConnection()) {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(/*some query*/);
if (rs.next()) {
return rs.getLong("transaction_id");
} else {
return 0L;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return null;
}
}
No, establishing a new physical connection to a database server is costly. It involves multiple steps: user authorization, establishing session defaults, allocating memory on both client and server, etc. This overhead should not be added to every single request.
It's a common practice to create a connection pool to share the physical connections between application threads. This introduces a concept of logical connections e.g. a Connection object created with DriverManager.getConnection() is a physical connection while DataSource.getConnection() returns a logical connection which is a proxy.
There are multiple database connection pooling libraries for Java that you can use e.g. HikariCP. Don't write your own, this is not simple.
Get fast data and deliver to client could be possible using the simplest way of using application.properties file. You may use this to get database connection to your datasource.
The error is that I cant open the connection to mysql database, it must be an error in parameters but I am confused , I have no idea where is the problem.
First you need to create a MySQL schema. Secondly, use JDBC to connect to your recently created database (via localhost - make sure you get the user/password right).
After that you should use DAO-like classes. I'll leave here a Connect class:
public class Connect {
private static final String USERNAME = "root";
private static final String PASSWORD = "12345";
private static final String URL = "localhost";
private static final String SCHEMA = "new_schema";
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection connect() throws SQLException {
return DriverManager.getConnection("jdbc:mysql://"+URL+"/"+SCHEMA+"?user="+USERNAME+"&password="+PASSWORD);
}
}
After you have the Connect class, you should connect to the database using Connection c = Connect.connect(). Here's a class that implements it.
public static List<Album> list() throws SQLException {
Connection c = Connect.connect();
ResultSet rs = c.createStatement().executeQuery("SELECT * FROM Albums");
List<Album> list = new ArrayList<>();
while (rs.next()) {
String name = rs.getString("nome"); // first table column (can also use 1)
String artist = rs.getString("artista"); // second table column (can also use 2)
Album a = new Album(name, artist);
list.add(a);
}
return list;
}
It should also give you an insight as to how you should use SQL commands.
If you'd like a more in-depth help you should post the code you used, otherwise it's difficult to give you a more "to-the-point" explanation.
JDBC URLs can be confusing. Suggest you try using a SQL tool that understands the JDBC protocol (such as the database development perspective in Eclipse) to validate the URL and make sure you can connect to the database before you start coding. Cutting and pasting a URL known to work into your code can avoid many problems.
I'm looking to authenticate with AD using unboundID but I get a serverdown error on the bind action.
The requests work fine when I do this via JNDI instead of unboundID.
I've looked it up online and most seem to have a similar way of doing things.
For e.g. I've referred this answer by Terry
LDAP: How to authenticate user with connection details
This is my code
public class LDAPSearch_unboundID {
private static final String LDAPIP = "hostname";
private static final int LDAPPORT = 636;
private static final String USERDN = "adminuser";
private static final String PASSWD = "adminpass";
private static final String SEARCHDN = "DC=AA,DC=BB,DC=CC";
public static void main(String[] args) {
authUser();
}
private static void authUser(String userid) {
BindRequest bindRequest = new SimpleBindRequest(SEARCHDN,PASSWD);
System.out.println("101");
try {
LDAPConnection ldapConnection = new LDAPConnection(LDAPIP,LDAPPORT);
//LDAPConnection ldapConnection = new LDAPConnection(lco, LDAPIP,LDAPPORT, SEARCHDN, PASSWD);
System.out.println("102");
BindResult bindResult = ldapConnection.bind(bindRequest);
System.out.println("103");
ResultCode resultCode = bindResult.getResultCode();
System.out.println("104");
if(resultCode.equals(ResultCode.SUCCESS))
{
System.out.println("Authentication SUCCESS!!!");
}
else
{
System.out.println("Authentication FAILED!!!");
}
ldapConnection.close();
}
catch (LDAPException e){
e.printStackTrace();
}
}
}
Here the output is
101
102
LDAPException(resultCode=81 (server down), errorMessage='The connection to server host:port was closed while waiting for a response to a bind request SimpleBindRequest(dn='DC=AA,DC=BB,DC=CC'): An I/O error occurred while trying to read the response from the server: java.net.SocketException: Connection reset')
at com.unboundid.ldap.sdk.SimpleBindRequest.handleResponse(SimpleBindRequest.java:718)
at com.unboundid.ldap.sdk.SimpleBindRequest.process(SimpleBindRequest.java:570)
at com.unboundid.ldap.sdk.LDAPConnection.bind(LDAPConnection.java:2150)
at ldapConnection.LDAPSearch_unboundID.authUser(LDAPSearch_unboundID.java:45)
at ldapConnection.LDAPSearch_unboundID.main(LDAPSearch_unboundID.java:34)
I've tried this with a couple of other AD servers. What am I doing wrong?
Update: I must add that I'm using an SSL connection.
I've tried updating the ldapconnection part of the code with this.
SSLSocketFactory socketFactory = sslUtil.createSSLSocketFactory();
LDAPConnection ldapConnection = new LDAPConnection(socketFactory);
ldapConnection.connect(LDAPIP,LDAPPORT);
System.out.println("102");
BindResult bindResult = ldapConnection.bind(bindRequest);
Now I get this error
101
102
LDAPException(resultCode=49 (invalid credentials), errorMessage='80090308: LdapErr: DSID-0C0903A9, comment: AcceptSecurityContext error, data 57, v1db1
at com.unboundid.ldap.sdk.LDAPConnection.bind(LDAPConnection.java:2178)
at ldapConnection.LDAPSearch_unboundID.authUser(LDAPSearch_unboundID.java:56)
at ldapConnection.LDAPSearch_unboundID.main(LDAPSearch_unboundID.java:39)
I can't see any documentation for "data 57" under result code 49.
The credentials are correct.
I use something like:
public static LDAPConnection getNewSSLConnection(String address, int port, BindRequest bindRequest) throws LDAPException, GeneralSecurityException
{
SSLUtil sslUtil = new SSLUtil(new TrustAllTrustManager());
SSLSocketFactory sslSocketFactory = sslUtil.createSSLSocketFactory();
LDAPConnection ldc = new LDAPConnection(sslSocketFactory);
ldc.connect(address, port);
ldc.bind(bindRequest);
return ldc;
}
DO NOT USE "TrustAllTrustManager() unless you are on a CLOSED TRUSTED network.
Find more ways for SSLUtil.
I have created a java login application with mysql database and deployed it on OpenShift.
URL: http://passwordbucket-king003.rhcloud.com/SimpleLoginTest/
while trying to running its simply showing a blank page with URL-http://passwordbucket-king003.rhcloud.com/SimpleLoginTest/loginservlet .But ideally it should show the home page. Here is my database connection file, please let me know that where I wrong?
package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConnection
{
// private final String host = "jdbc:mysql://localhost:3306/logindemo";
// private final String username = "root";
// private final String password = "root";
private final String OPENSHIFT_MYSQL_DB_PORT = "3306";
private final String OPENSHIFT_MYSQL_DB_HOST= "127.3.110.129";
private final String OPENSHIFT_MYSQL_DB_PASSWORD= "root";
private final String OPENSHIFT_MYSQL_DB_USERNAME= "root";
private final String OPENSHIFT_MYSQL_DB_URL= "jdbc:mysql://${OPENSHIFT_MYSQL_DB_HOST}:${OPENSHIFT_MYSQL_DB_PORT}/logindemo";
public Connection con = null;
public Connection openConnection()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
// con = DriverManager.getConnection(host, username, password);
con = DriverManager.getConnection(OPENSHIFT_MYSQL_DB_URL);
}
catch(ClassNotFoundException ex)
{
System.out.println(ex);
}
catch (SQLException ex)
{
System.out.println(getClass().getClass()+" = " +ex.toString());
}
return con;
}
public void closeConnection() throws SQLException
{
if(!con.isClosed()){
con.close();
}
}
}
First of all, on OpenShift your application should be deployed as ROOT.war and there should be content at the "/" context.
Second, if you want to read and use environment variables, you should use System.getenv(). For example:
private final String OPENSHIFT_MYSQL_DB_HOST = System.getenv("OPENSHIFT_MYSQL_DB_HOST);
Your connection URL should also contain the credentials given to you when you created the database.
The database name is probably "passwordbucket" the same as your application name.
In stead of using a jdbc connection you should consider using the JNDI datasource that comes preconfigured on OpenShift. Please check the documentation. You disd not specify the container type you are using.
I am trying to create a connection using connectionFactory.createConnection() method but it returns a null.
Below is my code :
#Stateless
public class test
{
#Resource(mappedName = "java:comp/DefaultJMSConnectionFactory")
private static ConnectionFactory connectionFactory;
#Resource(mappedName = "jdbc/JurassicParkCon")
private static Queue queue;
public static void main(String args[])
{
Connection connection = null;
Session session = null;
MessageProducer messageProducer = null;
TextMessage message = null;
final int NUM_MSGS = 3;
try {
connection = connectionFactory.createConnection();
}
catch(Exception e){System.out.println("It is:"+e.getMessage());}
In the above code am only trying to create a connection but it returns NullPointerException. I have added a JMS resource through the admin console in GlassFish (name is jdbc/JurassicParkCon).
Recently only I started working with EJB's so I am not very familiar with errors. I have added the #Stateless annotation because there was a similar problem which was posted on StackOverflow and for that user adding the annotation worked but not for me.
What might be the problem here ?
Thank you for your time.
It won't work as a standalone application. You need to run it in a container.