Olivier Catteau
Olivier Catteau

Reputation: 64

Set max-connections on MysqlConnectionPoolDataSource

I use MysqlConnectionPoolDataSource. I had to migrate from jndi definitions to a custom solution. The old definition looks like :

<data-source                                                              
    name="MySqlDS" location="jdbc/MySqlPooledDS"                             
    class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"       
    max-connections="100"                                                    
    min-connections="5"                                                      
    inactivity-timeout="200"                                                 
    wait-timeout="10"                                                        
    username="USERNAME"                                                      
    password="PASSWORD"                                                      
    url="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=Cp1251"
/>

And I would like to replace it with this code :

DataSource dataSource = (DataSource) Class.forName(driver.getType()).newInstance();
new Statement(dataSource, "setUrl", new Object[]{ driver.getUrl().getValue() }).execute();
new Statement(dataSource, "setUser", new Object[]{ driver.getUser().getValue() }).execute();
new Statement(dataSource, "setPassword", new Object[]{ driver.getPassword().getValue() }).execute();
new Statement(dataSource, "setUseUnicode", new Object[]{ "yes".equals(driver.getUseUnicode().getValue()) }).execute();
new Statement(dataSource, "setCharacterEncoding", new Object[]{ driver.getCharacterEncoding().getValue() }).execute();

I would like to define the max-connections property on the dataSource but I don't know how to do it. Any idea ?

Thanks.

Upvotes: 0

Views: 3695

Answers (2)

user987339
user987339

Reputation: 10707

You'll have to put it in connection string: "jdbc:mysql://localhost:3306/test?max-connections=100..."

Upvotes: 1

NickDK
NickDK

Reputation: 5197

Your replacement code is a bit weird, why don't you use a concrete implementation of a Datasource? Then you can set the url, user etc directly using the appropriate setters.

i would suggest to use something like c3p0 or BoneCP to set-up a connection pool, can be done for MySQL without too much hassle.

BoneCP

c3p0

Upvotes: 0

Related Questions