【发布时间】:2015-12-30 21:21:07
【问题描述】:
我正在尝试使用返回类型 Connection 的 spring jdbc 进行简单连接,在我的项目中,我正在使用 spring jdbc 和 spring 数据,自动配置。
在我的代码中,我需要返回这个连接(以及我的本地信息)。
有可能吗?在这种情况下,是否可以获得目前正在使用的信息? (我的意思是,dbname,密码等等..)
谢谢
编辑------
原来的dataSource bean是这样的
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="jdbc:mysql://${jdbc.host}:${jdbc.port}"/>
</bean>
但是我不能用注释来实现,有什么想法吗?
我已经试过了
DriverManagerDataSource source = new org.springframework.jdbc.datasource.DriverManagerDataSource();
错误
我不断得到这个
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required
这个类扩展了JdbcDaoSupport,似乎它需要它......
这里是 JdbcDaoSupport.class
package org.springframework.jdbc.core.support;
import java.sql.Connection;
import javax.sql.DataSource;
import org.springframework.dao.support.DaoSupport;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.support.SQLExceptionTranslator;
public abstract class JdbcDaoSupport extends DaoSupport {
private JdbcTemplate jdbcTemplate;
public JdbcDaoSupport() {
}
public final void setDataSource(DataSource dataSource) {
if(this.jdbcTemplate == null || dataSource != this.jdbcTemplate.getDataSource()) {
this.jdbcTemplate = this.createJdbcTemplate(dataSource);
this.initTemplateConfig();
}
}
protected JdbcTemplate createJdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
public final DataSource getDataSource() {
return this.jdbcTemplate != null?this.jdbcTemplate.getDataSource():null;
}
public final void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
this.initTemplateConfig();
}
public final JdbcTemplate getJdbcTemplate() {
return this.jdbcTemplate;
}
protected void initTemplateConfig() {
}
protected void checkDaoConfig() {
if(this.jdbcTemplate == null) {
throw new IllegalArgumentException("\'dataSource\' or \'jdbcTemplate\' is required");
}
}
protected final SQLExceptionTranslator getExceptionTranslator() {
return this.getJdbcTemplate().getExceptionTranslator();
}
protected final Connection getConnection() throws CannotGetJdbcConnectionException {
return DataSourceUtils.getConnection(this.getDataSource());
}
protected final void releaseConnection(Connection con) {
DataSourceUtils.releaseConnection(con, this.getDataSource());
}
}
我已将此类声明为 @bean 并执行此操作
public DriverManagerDataSource provideSource() {
DriverManagerDataSource dataSource = new org.springframework.jdbc.datasource.DriverManagerDataSource();
//this.dataSource = dataSource;
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUsername("user");
dataSource.setPassword("pass");
dataSource.setUrl("jdbc:mysql://localhost:3306/db");
return dataSource;
}
@Bean
MyClientDao myClientDao(){
MyClientDao myClientDao = new MyClientDao();
myClientDao().setDatabaseName("db");
myClientDao().setDataSource(provideSource());
return myClientDao();
}
知道怎么做吗?
【问题讨论】:
-
请问您为什么需要这样做? documentation 似乎表明处理连接是 Spring 的工作......
-
我发现一些代码(我无法更改)需要它来进行锁定,我认为它可以去检查连接池,但似乎需要将其作为参数发送方法调用
-
嗯,我无法完全理解您的上下文,但this question 的答案(与您的非常相似,重复?!)建议您可以使用DataSourceUtils
-
谢谢@Morfic,我更新了我的问题,你能看看吗?我试过其他帖子的回复,但没有结果。
-
不确定
I cannot make it with annotations是什么意思,但你说你正在使用spring,所以你不应该尝试自己实例化数据源。相反,您可以简单地在 bean 中定义一个自动装配字段:@Autowired private DataSource dataSource;。
标签: spring jdbc spring-jdbc