0

I need to catch errors during authentication (like wrong parameters). I find nothing about it. I have isolted the procedure with threads. But with this bad way, the user can't understand what goes wrong

Below, my code:

public static boolean access(String db, String ip, String usr, String pwd){
    Map<String, String> persistenceMap = new HashMap<>();

    persistenceMap.put("hibernate.ogm.datastore.database", db);
    persistenceMap.put("hibernate.ogm.datastore.host", ip);
    persistenceMap.put("hibernate.ogm.datastore.username", usr);
    persistenceMap.put("hibernate.ogm.datastore.password", pwd);

    Thread mainThread = Thread.currentThread();
    Thread logThread = new Thread(() -> {
        Connection.EMF = Persistence.createEntityManagerFactory("ogm-jpa-mongo", persistenceMap);
        Connection.EM = Connection.EMF.createEntityManager();
        Connection.isOpen = true;
    });
    Thread timeOut = new Thread( () -> {
        try{ Thread.sleep( 5000 ); }
        catch(InterruptedException ex){ }
        mainThread.interrupt();
    });


    logThread.start();
    timeOut.start();

    try{ logThread.join(); }
    catch(InterruptedException ex){ return false; }

    Connection.TM = com.arjuna.ats.jta.TransactionManager.transactionManager();

    return Connection.isOpen;
}
Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30

1 Answers1

0

If the credentials are wrong, an org.hibernate.HibernateException is thrown.

The cause of this exception contains the details returned by mongodb and it's the only way to figure out what happened.

Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30
  • That's true, but i can't catch it with the classic try-catch construct. –  Jun 29 '17 at 19:13
  • You don't have a try-catch block in your example that contains the factory creation – Davide D'Alto Jul 11 '17 at 13:15
  • I did not insert it because it is useless. The exception is thrown and managed without I can do anything –  Jul 11 '17 at 13:25
  • You're right, I'm sorry. I thought it was useless because in case of error the waiting time is exaggerated. Would you know a way to reduce it? –  Jul 11 '17 at 13:32
  • I don't know what you mean by waiting time, if you mean the connection time out, you can try to set this propertiy: hibernate.ogm.mongodb.driver.connectionTimeout. You can find a list of properties in the documentation: https://docs.jboss.org/hibernate/stable/ogm/reference/en-US/html_single/#_configuring_mongodb – Davide D'Alto Jul 11 '17 at 13:55