2016-04-21 99 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