开发者

handle Hibernate incorrect connection on Swing app

I got a Swing app that start asking the user for the connection parameters, as every user can make mistakes I want to know the best way to handle when they make a wrong connection attempt i.e wrong url

what I got is a button that sends dbType, user, pass and url to a Map and then I obtain an EntityManagerFactory that last the whole application like this:

emf = Persistence.createEntityManagerFactory("persistenceUnit", connectionProperties);

however is taking almost 1 min in telling me

java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection

  • Can I manipulate Hibernate to answer timeout faster?
  • Should I change the way I'm using the entityManagerFactory?

EDIT: I have a Connector class that all it does is allow the GUI to send the parameters to it and then allows my DAO to retrieve an instance of that class to obtain the EntityManagerFactory. These are all my connection properties, the GUI changes the dialect based on the DB it picks.:

connectionProperties.put("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect"); connectionProperties.put("hibernate.connection.driver_class", "oracle.jdbc.driver.OracleDriver"); connectionProperties.put("hibernate.connection.url", userSettings.get("url", "")); connectionProperties.put("hibernate.connection.username", userSettings.get("username", "")); connectionProperties.put("hibernate.connection.password", userSettings.get("password", ""));

and this is the method that provides an EntityManager to whoever needs one:

protected EntityManager getEntityManager() { if (em == null) {

em = 开发者_JAVA百科Connector.getInstance().getEmf().createEntityManager();

return em; } else { return em; } }


I have to say I'm surprised that it appears the only way to set a JDBC connection timeout is with the static DriverManager.setLoginTimeout(). I expected to find some timeout support in the popular connection pools, but it seems not to exist except in C3P0's non-pooling DataSource implementation, which you wouldn't want to use for a production app.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜