vibur / vibur-dbcp

Vibur DBCP - concurrent and dynamic JDBC connection pool
http://www.vibur.org
Apache License 2.0
96 stars 11 forks source link

Hibernate + Vibur + JNDI Datasource #14

Closed rlmf82 closed 3 years ago

rlmf82 commented 4 years ago

Hi guys,

 I would like a help to configure my java application so it can use Hibernate with Vibur connecting my database through a JNDI Datasource.

 My application is already working without vibur. But when I insert vibur properties to my hibernate.cfg.xml it doesn't work. 

My CFG is like below:

` org.postgresql.Driver

    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.connection.datasource">java:comp/env/jdbc/myDatasource</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">10</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.PostgreSQL94Dialect</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Vibur DBCP specific properties -->
    <property name="hibernate.connection.provider_class">
        org.vibur.dbcp.integration.ViburDBCPConnectionProvider
    </property>

    <property name="hibernate.vibur.poolInitialSize">1</property>
    <property name="hibernate.vibur.poolMaxSize">300</property>

    <property name="hibernate.vibur.connectionIdleLimitInSeconds">2</property>
    <property name="hibernate.vibur.testConnectionQuery">isValid</property>

    <property name="hibernate.vibur.logQueryExecutionLongerThanMs">500</property>
    <property name="hibernate.vibur.logStackTraceForLongQueryExecution">true</property>

    <property name="hibernate.vibur.statementCacheMaxSize">200</property>`

After execute my Tomcat 8.5 server a exception is returned like below:

org.vibur.dbcp.ViburDBCPException: Unexpected type for configuration property externalDataSource/java:comp/env/jdbc/myDatasource at org.vibur.dbcp.ViburDBCPDataSource.configureFromProperties(ViburDBCPDataSource.java:198) at org.vibur.dbcp.ViburDBCPDataSource.<init>(ViburDBCPDataSource.java:125) at org.vibur.dbcp.integration.ViburDBCPConnectionProvider.configure(ViburDBCPConnectionProvider.java:63) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:241) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:209) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88) at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:258) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:232) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:209) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:241) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:209) at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:356) at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:112) at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:84) at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:470) at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:91) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691) at util.HibernateUtil.buildSessionFactory(HibernateUtil.java:22) at util.HibernateUtil.<clinit>(HibernateUtil.java:11) at empresa.ControladorEmpresa.<init>(ControladorEmpresa.java:44) at empresa.ControladorEmpresa.getInstance(ControladorEmpresa.java:38) at util.fachadas.Fachada.recuperarUrlCompletaEmpresa(Fachada.java:1085) at util.web.FiltroAcesso.doFilter(FiltroAcesso.java:38) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:610) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:660) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:808) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748)

My Context.xml is configured like:

<Resource auth="Container" driverClassName="org.postgresql.Driver" global="jdbc/myDatasource" maxIdle="10" maxTotal="20" maxWaitMillis="-1" name="jdbc/myDatasource" username="postgres" password="123456" type="javax.sql.DataSource" url="jdbc:postgresql://localhost:5432/Database"/>

anyone could you help me with this problem? Or say how to define an externalDataSource for vibur?

simeonmalchev commented 4 years ago

@rlmf82 it seems to me that the content of your hibernate.cfg.xml is wrong but I can't be sure. Can you please compare your file with various examples of such file? I found one here: https://dzone.com/articles/hibernate-5-xml-configuration-example . Also, the http://vibur.org docs have an example snippet: http://www.vibur.org/#vibur-and-hibernate . Try to work from these example to figure out what exactly is misconfigured or your end. If you can't figure it out, post here your complete hibernate.cfg.xml and I'll try to help.

rlmf82 commented 4 years ago

Hi Simeon,

Thanks for your attention. Firstly, my application is already working with Vibur, but my connection information are all together at hibernate.cfg.xml, like showed in the articles that you sent to me.

`com.mysql.cj.jdbc.Driver

jdbc:mysql://localhost:3306/hibernate_db?useSSL=false root root` But, what I want is to configure a JNDI on my server and configure my hibernate to use it. Like that: ` java:comp/env/jdbc/**myDatasource** ` When i replace those hibernate connection properties by this above, the error throwed. If I keep the connection information, it works. Additionally, If I comment vibur properties and run my server, both ways of connection(by jndi and traditional) works fine. I have already tried to create a external datasource to Vibur, but I didn't find a place explaining how to do it.
simeonmalchev commented 4 years ago

@rlmf82 sorry for my slow reply way too many things are happening at the moment. Based on this line in the stacktrace at org.vibur.dbcp.integration.ViburDBCPConnectionProvider.configure(ViburDBCPConnectionProvider.java:63) I believe you're using one of the old vibur-dbcp-hibernate5 artefacts to integrate Hibernate and Vibur. Can you please switch to hibernate-vibur artefact that is part of the standard Hibernate distribution? See here http://www.vibur.org/#hibernate-integration-artifacts . Possibly upgrade to latest Hibernate 5.4.x version, too. For the Tomcat->Hibernate->JNDI configuration check docs like https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#jndi or this or this. I haven't tried to follow these examples, I cannot tell how good they are.

simeonmalchev commented 3 years ago

Closing due to inactivity.