Java Spring+hibernate vs hibernate 配置。UnsupportedOperationException 的原因:BasicDataSource 不支持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24672619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring+hibernate vs hibernate configuration. Cause of UnsupportedOperationException: Not supported by BasicDataSource
提问by gstackoverflow
Initially I used only hibernate
最初我只使用休眠
And I had following hibernate.cfg.xml
:
我有以下内容hibernate.cfg.xml
:
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">create</property>
<property name="connection.url">jdbc:mysql://localhost:3306/...</property>
<property name="connection.username">root</property>
<property name="connection.password">XXX</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="connection.pool_size">1</property>
<property name="current_session_context_class">thread</property>
//mapping
...
</session-factory>
</hibernate-configuration>
And It works good:
它运作良好:
after I include Springand then configuration look so:
在我包含Spring 之后,然后配置看起来像这样:
...
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/..." />
<property name="username" value="root" />
<property name="password" value=XXX />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
...
after it I see in console:
之后我在控制台中看到:
java.lang.UnsupportedOperationException: Not supported by BasicDataSource
at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1432)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
After I tried to remove
在我尝试删除之后
<property name="connection.username">root</property>
<property name="connection.password">XXX</property>
from hibernate.cfg.xml
and I don't see exceptions.
从hibernate.cfg.xml
,我没有看到例外。
Can you explain what is the cause of this problems?
你能解释一下这个问题的原因是什么吗?
Initially I thought that problem that I shouldn't duplicate information in different configurations but now I see that for example url
define inside dataSource
and inside hibernate.cfg.xml
最初我认为我不应该在不同配置中重复信息的问题,但现在我看到例如url
定义内部dataSource
和内部hibernate.cfg.xml
Please clarify this Spring + Hibernatemagic.
请澄清这个Spring + Hibernate魔法。
采纳答案by Keval Trivedi
In latest version getConnection(User,Password) method is not supported.
在最新版本中不支持 getConnection(User,Password) 方法。
It will helps you : Replace
它将帮助您:更换
org.apache.commons.dbcp2.BasicDataSource
with
和
org.springframework.jdbc.datasource.DriverManagerDataSource
回答by user1219387
I had this same issue and after above solution I got a destroy method 'close' not found error or something along those lines.
我遇到了同样的问题,在上述解决方案之后,我得到了一个销毁方法“关闭”未找到错误或类似的东西。
However, it does appear that getConnection(user, password) is the underlying issue here. When I commented out the connection.username and connection.password in the hibernate.cfg.xml everything works fine.
但是,似乎 getConnection(user, password) 是这里的潜在问题。当我在 hibernate.cfg.xml 中注释掉 connection.username 和 connection.password 时,一切正常。
Correction: I should clarify that I moved the user name and password to the appContext like so
更正:我应该澄清我将用户名和密码移动到 appContext 像这样
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://127.0.0.1:3306/my_schema"
p:username="root"
p:password="mypassword">
</bean>
回答by Teixi
Be aware that with:
请注意:
org.springframework.jdbc.datasource.DriverManagerDataSource
You're not provided with connection pooling!
您没有提供连接池!
I would recommend to move to Tomcat JDBC Connection Pool.
我建议转移到 Tomcat JDBC 连接池。
That is by now IMHO the most efficient one.
到目前为止,恕我直言,这是最有效的一种。
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
It requires the dependency
它需要依赖
org.apache.tomcat tomcat-jdbc
Check documentation: https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html
检查文档:https: //tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html
回答by Vic
I had the same problem. The BasicDataSource
class will connect using its own url
, username
and password
parameters. Session factory will use already configured data source. Therefore as a result the url
, username
and password
parameters are overridden. Just ditch those connection parameters in hibernate.cfg.xml
because they are managed by the dataSource
bean.
我有同样的问题。该BasicDataSource
班将连接使用它自己的url
,username
和password
参数。会话工厂将使用已配置的数据源。因此,url
,username
和password
参数被覆盖。只需丢弃这些连接参数,hibernate.cfg.xml
因为它们是由dataSource
bean管理的。