Hibernate Jndi Tomcat Error : Not supported by BasicDataSource
Hibernate Jndi Tomcat Error : Not supported by BasicDataSource
我实际上试图通过 JNDI 设置休眠和 jdbc 连接池,但我没有成功。经过几个小时的研究,我来找你,也许有人知道答案。
首先要做的事情:我正在使用 Tomcat 8.0 和 Hibernate 5.2.8。我实际上不能交替使用那些,如果那需要让它工作的话。
我得到的错误是:"java.lang.UnsupportedOperationException: Not supported by BasicDataSource"
到目前为止我发现了什么:由于 DatasourceConnectionProviderImpl.getConnection 的变化,错误似乎发生了(我不是 100% 确定),其中 "getConnection(Username, Password" 是 'depricated'(实际上,它只是抛出这个错误)。
但这里有问题,即使我不传递任何参数,我也会得到这个异常。我试图制作自己的 "javax.sql.DataSource" 界面来克服这个问题,但这也无济于事。
另一件事:JDBC 工作正常,唯一的问题是休眠。
跟进上下文、网络和 class 信息:
上下文:
<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"
/>
网站:
<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>
代码:
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();
}
}
如果需要更多信息,我很乐意提供
伊迪丝:
根据要求:
<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>
可能和这里提到的一样:tomcat-connection-pooling-java-lang-unsupportedoperationexception-not-support
其中一个答案的总结:
如果 hibernate 确实在类路径中找到属性 hibernate.connection.username
or/and hibernate.connection.password
,它会使用已弃用的 getConnection(user,password)
方法。
我实际上试图通过 JNDI 设置休眠和 jdbc 连接池,但我没有成功。经过几个小时的研究,我来找你,也许有人知道答案。
首先要做的事情:我正在使用 Tomcat 8.0 和 Hibernate 5.2.8。我实际上不能交替使用那些,如果那需要让它工作的话。
我得到的错误是:"java.lang.UnsupportedOperationException: Not supported by BasicDataSource"
到目前为止我发现了什么:由于 DatasourceConnectionProviderImpl.getConnection 的变化,错误似乎发生了(我不是 100% 确定),其中 "getConnection(Username, Password" 是 'depricated'(实际上,它只是抛出这个错误)。
但这里有问题,即使我不传递任何参数,我也会得到这个异常。我试图制作自己的 "javax.sql.DataSource" 界面来克服这个问题,但这也无济于事。
另一件事:JDBC 工作正常,唯一的问题是休眠。
跟进上下文、网络和 class 信息:
上下文:
<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"
/>
网站:
<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>
代码:
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();
}
}
如果需要更多信息,我很乐意提供
伊迪丝:
根据要求:
<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>
可能和这里提到的一样:tomcat-connection-pooling-java-lang-unsupportedoperationexception-not-support
其中一个答案的总结:
如果 hibernate 确实在类路径中找到属性 hibernate.connection.username
or/and hibernate.connection.password
,它会使用已弃用的 getConnection(user,password)
方法。