2016-04-21 134 views
0

我知道这将是重复的问题,但我觉得我的问题有点不同。太多的连接弹簧引导jdbc

我JdbcDAO类,如

@Component 
    public class JdbcUserDAO implements UserDAO { 
    @Autowired 
    MyJdbc myJdbc; 
    } 

我已经如下定义MyJdbc类:

@Component 
public class MyJdbc { 

@Autowired 
    protected JdbcTemplate jdbc; 

} 

在MyJdbc类我定义的插件和BATCHUPDATE,并呼吁他们通过JDBC变量。 它会创建太多的连接异常吗?

我在application.properties文件中定义的JDBC参数:

spring.datasource.url=#databaseurl 
spring.datasource.username=#username 
spring.datasource.password=#password 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 
spring.datasource.test-on-borrow=true 
spring.datasource.max-active=100 
spring.datasource.max-wait=10000 
spring.datasource.min-idle=10 
spring.datasource.validation-query=SELECT 1 
spring.datasource.time-between-eviction-runs-millis= 5000 
spring.datasource.min-evictable-idle-time-millis=30000 
spring.datasource.test-while-idle=true 
spring.datasource.test-on-borrow=true 
spring.datasource.test-on-return=false 

我得到异常:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Too many connections 

我已经做了很多更改application.properties文件的各种常数但它不起作用。我的数据库托管在AWS RDS上。

但对于更新团块图像值我做的:

blob= myJdbc.jdbc.getDataSource().getConnection().createBlob(); 
      blob.setBytes(1, str.getBytes()); 
      pstmt = myJdbc.jdbc.getDataSource().getConnection().prepareStatement("update user_profile set profileImage=? where user_profile.id in (select id from user_login where email=?)"); 
+3

错误来自MySQL,而不是您的应用程序,您知道您的RDS实例能够应对多少个连接吗?我的猜测是它小于100. –

+0

你有没有理由使用普通的Jdbc API而不是Spring Data或JPA? –

+0

@DaveBower:我确实显示了像'max_connections'这样的全局变量;它显示66 – Chetan

回答

2
blob= myJdbc.jdbc.getDataSource().getConnection().createBlob(); 
blob.setBytes(1, str.getBytes()); 
pstmt = myJdbc.jdbc.getDataSource().getConnection().prepareStatement("update user_profile set profileImage=? where user_profile.id in (select id from user_login where email=?)"); 

问题是与你的代码。该代码打开到数据库的其他连接而不关闭它们。你自己打开连接,然后你也应该关闭它们。但在这些情况下最好使用ConnectionCallback

myJdbc.execute(new ConnectionCallback() { 
    public Object doInConnection(Connection con) throws SQLException, DataAccessException { 
     blob = con.createBlob(); 
     blob.setBytes(1, str.getBytes()); 
     pstmt = con.prepareStatement("update user_profile set profileImage=? where user_profile.id in (select id from user_login where email=?)"); 
     return null; 
    } 
}); 

然而,它更容易使用Spring JDBC Blob支持(请参阅the reference guide)。这样你就不需要自己搞砸连接和blob。

final String query = "update user_profile set profileImage=? where user_profile.id in (select id from user_login where email=?)"; 
myJdbc.jdbc.execute(query, new AbstractLobCreatingPreparedStatementCallback(lobHandler) { 1 
    protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException { 
     byte[] bytes = str.getBytes(); 
     ps.setString(2, email); 
     lobCreator.setBlobAsBinaryStream(ps, 1, str.getBytes()); 
    } 
}); 
+0

我会尝试这一个,但是我使用自定义的DAO类来查询是正确的还是打开多个连接。 – Chetan

+0

使用jdbc模板...每个你自己做'getCOnnection'的地方(基本上绕过spring及其事务和资源管理),你自己正在管理连接。正确使用JdbcTemplate。与框架一起工作不在其周围。 –

+0

如在JDBC blob支持中所见,如何读回blob的存储值并将其转换回字符串。 – Chetan