0

I'm using swing with hibernate.
So when I enter button it connect but again I press the button it does repeating connectivity and that take few seconds to perform connectivity.
I do not use @annotations

    SessionFactory sessionFactory = new
         Configuration().configure().buildseseienFactery ();

public Registration Login(String username, String password) {
Session session =  sessionFactory.openSession ();
Criteria criteria = session.createCriteria(Registration.cLass);
criteria.add(Restrictions.eq ("username", username));
criteria.add (Restrictions.eg ("password", password));

Registration registration (Registration) criteria.uniqueResult();
session.close();
return registration;
}

I supposed above block performing connectivity only once but when Login() method call it does perform again.
So how can I perform connectivity only once?

Log is as following:

Sep 15, 2019 12:07:21 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Sep 15, 2019 12:07:21 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.1.Final}
Sep 15, 2019 12:07:21 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Sep 15, 2019 12:07:21 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Sep 15, 2019 12:07:21 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Sep 15, 2019 12:07:21 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Sep 15, 2019 12:07:22 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/cpos/pojo/Registration.hbm.xml
Sep 15, 2019 12:07:22 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/cpos/pojo/Sales.hbm.xml
Sep 15, 2019 12:07:22 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/cpos/pojo/Items.hbm.xml
Sep 15, 2019 12:07:22 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/cpos/pojo/Customers.hbm.xml
Sep 15, 2019 12:07:22 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/cpos/pojo/Receivings.hbm.xml
Sep 15, 2019 12:07:22 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/cpos/pojo/Salestransaction.hbm.xml
Sep 15, 2019 12:07:22 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/cpos/pojo/Suppliers.hbm.xml
Sep 15, 2019 12:07:22 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/cpos/pojo/Rctransaction.hbm.xml
Sep 15, 2019 12:07:22 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Sep 15, 2019 12:07:22 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH010002: C3P0 using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/CPOS?zeroDateTimeBehavior=convertToNull
Sep 15, 2019 12:07:22 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH000046: Connection properties: {user=root}
Sep 15, 2019 12:07:22 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH000006: Autocommit mode: false
Sep 15, 2019 12:07:22 PM com.mchange.v2.log.MLog <clinit>
INFO: MLog clients using java 1.4+ standard logging.
Sep 15, 2019 12:07:22 PM com.mchange.v2.c3p0.C3P0Registry banner
INFO: Initializing c3p0-0.9.2.1 [built 20-March-2013 10:47:27 +0000; debug? true; trace: 10]
Sep 15, 2019 12:07:22 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@5aa87035 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@27bcd446 [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> z8kfsxa5ebtiu21xhrse6|7a3dc7, idleConnectionTestPeriod -> 0, initialPoolSize -> 5, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 1800, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 50, maxStatementsPerConnection -> 0, minPoolSize -> 5, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@cfc29f4 [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> z8kfsxa5ebtiu21xhrse6|1a8ed8f, jdbcUrl -> jdbc:mysql://localhost:3306/CPOS?zeroDateTimeBehavior=convertToNull, properties -> {user=******} ], preferredTestQuery -> null, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, factoryClassLocation -> null, identityToken -> z8kfsxa5ebtiu21xhrse6|12fe1cf, numHelperThreads -> 3 ]
Sep 15, 2019 12:07:23 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Sep 15, 2019 12:07:23 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
Sep 15, 2019 12:07:23 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Sep 15, 2019 12:07:23 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory

Is there any reason for delaying? How to get rid of it?

Swapnil
  • 115
  • 1
  • 12

1 Answers1

1

Most probably it is because you are creating a new SessionFactory whenever you click the button.

There should be one SessionFactory for the whole application . Create it when the application start using singleton pattern or DI framework (e.g. Spring) etc..The idea is to ensure only one SessionFactory instance is created .

If the codes need to access the SessionFactory to do their work , they should always access this SessionFactory instance rather than create a new one.

Ken Chan
  • 84,777
  • 26
  • 143
  • 172
  • Thank you so much I thought that is not a case. But would you tell me how to create it when application start? – Swapnil Sep 15 '19 at 07:18
  • You are welcome but I think it is the 99% chance it is the case if you always see the same log that you posted in the question appears again and agin. To create it when application start , you can refer to the singleton pattern link that I share with you... Good luck. – Ken Chan Sep 15 '19 at 07:23