Search code examples
javahibernatetomcatjndi

Hibernate Jndi Tomcat Error : Not supported by BasicDataSource


im actually trying to set up a hibernate and jdbc connection pooling by JNDI, but i fail to success to do so. After hours of research, i come to you, maybe someone does know the answer.

First things first: I`m using Tomcat 8.0 and Hibernate 5.2.8. I cant actually alternate in those, if thats required to make it work.

The error im getting is : "java.lang.UnsupportedOperationException: Not supported by BasicDataSource"

What i found out so far : The error seem to occur (im not 100% sure) due to a change in the DatasourceConnectionProviderImpl.getConnection, where "getConnection(Username, Password" is 'depricated' (Actually, it simply throws this error).

But here`s the catch, even if i dont transfer any parameters, i get this exception. I tried to make my very own Interface of "javax.sql.DataSource" to overcome this, but that didnt help either.

Another thing : JDBC works just fine, the only problem is hibernate.

Following up the context, web and class infos :

Context :

<Resource name="jdbc/basic"
        global="jdbc/basic"
        auth="Container" 
        type="javax.sql.DataSource" 
        maxActive="10"
        maxIdle="20"
        maxWait="10000"
        username="root"
        password="1" 
        driverClassName="com.mysql.jdbc.Driver"
        testOnBorrow="true"
        url="jdbc:mysql://localhost/javatest"
    />

Web :

  <resource-ref>
      <description>JNDI MYSql JDBC Connection</description>
      <res-ref-name>jdbc/basic</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>

Code :

public TestClass() {
    try {
         Connection jdbc = ((javax.sql.DataSource) new InitialContext().lookup("java:comp/env/jdbc/basic")).getConnection();
         System.out.println(new Configuration().configure("managedbeans/hibernate.cfg.xml").buildSessionFactory().openSession().createQuery("Select * from Testdata").list().size());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

if anymore information is required, im happy to provide

Edith:

As Requested :

<hibernate-configuration> 
    <session-factory>

        <property name="hibernate.connection.datasource">java:comp/env/jdbc/basic</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property> 
        <property name="current_session_context_class">thread</property>
        <property name="hbm2ddl.auto">update</property> 
        <property name="hibernate.max_fetch_depth">3</property>

        <!-- Mapped annotated entity-->
        <mapping class="managedbean.TestPojo"/>

    </session-factory> 
</hibernate-configuration>

Solution

  • Possibly it is the same as mentioned here: tomcat-connection-pooling-java-lang-unsupportedoperationexception-not-support

    Summary of one of the answers: If hibernate does find the properties hibernate.connection.username or/and hibernate.connection.password within the classpath it uses the deprecated getConnection(user,password) method.