Reputation: 37078
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.
Upvotes: 2
Views: 13433
Reputation: 1888
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.
Upvotes: 2
Reputation: 1097
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.
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
Upvotes: 3
Reputation: 135
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.
Correction: I should clarify that I moved the user name and password to the appContext like so
<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>
Upvotes: 1
Reputation: 1300
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
Upvotes: -3