Spring + Hibernate vs hibernate配置。 UnsupportedOperationException的原因:BasicDataSource不支持 [英] Spring+hibernate vs hibernate configuration. Cause of UnsupportedOperationException: Not supported by BasicDataSource

查看:166
本文介绍了Spring + Hibernate vs hibernate配置。 UnsupportedOperationException的原因:BasicDataSource不支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初我只用了hibernate



我有以下 hibernate.cfg.xml

 < hibernate-configuration> 
< session-factory>
< property name =hbm2ddl.auto>建立< / property>
< property name =connection.url> jdbc:mysql:// localhost:3306 /...& lt; / 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>线程< / property>

//映射
...

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

而且效果很好:

我包含 Spring ,然后配置如下:

  ... 
destroy-method =close>
< property name =driverClassNamevalue =com.mysql.jdbc.Driver/>
< property name =urlvalue =jdbc:mysql:// localhost:3306 / .../>
< property name =usernamevalue =root/>
< property name =passwordvalue = XXX />
< / bean>

< bean id =sessionFactory
class =org.springframework.orm.hibernate4.LocalSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =configLocationvalue =classpath:hibernate.cfg.xml/>
< / bean>
...

之后我在控制台中看到:

  java.lang.UnsupportedOperationException:不受BasicDataSource支持
在org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1432)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)

在我试图删除

 < property name =connection.username> root< / p&性> 
< property name =connection.password> XXX< / property>

来自 hibernate.cfg.xml 和I没有看到异常。



你能解释这个问题的原因吗?



最初我以为那个问题,我不应该在不同的配置中重复信息,但现在我发现例如 url 定义在 dataSource 中并且里面 hibernate.cfg.xml



请说明这个 Spring + Hibernate 的魔术。 / b>

解决方案

在最新版本中,不支持getConnection(用户,密码)方法。



它可以帮助你:
替换

  org.apache.commons.dbcp2.BasicDataSource 

with

  org.springframework.jdbc.datasource.DriverManagerDataSource 


Initially I used only hibernate

And I had following 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 Spring and then configuration look so:

...
<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.

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

Please clarify this Spring + Hibernate magic.

解决方案

In latest version getConnection(User,Password) method is not supported.

It will helps you : Replace

org.apache.commons.dbcp2.BasicDataSource

with

org.springframework.jdbc.datasource.DriverManagerDataSource

这篇关于Spring + Hibernate vs hibernate配置。 UnsupportedOperationException的原因:BasicDataSource不支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆