Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support networkTimeout on built-in DataSource classes #1527

Closed
giridharkannan opened this issue Apr 23, 2019 · 9 comments

Comments

@giridharkannan
Copy link

commented Apr 23, 2019

I would like to have an ability to set JDBC connection's networkTimeout via configuration property. Am well aware of the existing defaultStatementTimeout property, but this is not the equivalent of networkTimeout

The defaultStatementTimeout sets JDBC's Statement.setQueryTimeout which tries to cancel the query by sending control command through the existing connection. This cannot even act as an workaround for the situation when the DB server's network goes down, which would result in stalling of all the DB request threads.

@giridharkannan

This comment has been minimized.

Copy link
Author

commented Apr 30, 2019

Any update on this feature would be highly appreciated. Thanks.

@harawata

This comment has been minimized.

Copy link
Member

commented Apr 30, 2019

Hi @giridharkannan ,

I think network timeout should be an option for a data source rather than MyBatis.
Or, do you want a new option networkTimeout in the internal PooledDataSource and UnpooledDataSource ? Then we would consider it.

Besides, many drivers support socketTimeout as a connection property.
Is there any reason why you prefer setNetworkTimeout over socketTimeout?

@giridharkannan

This comment has been minimized.

Copy link
Author

commented Apr 30, 2019

I would love to have the option to set networkTimeout in UnpooledDataSource, that's the workaround that we use currently.

The reason why I feel this is better when compared with socketTimeout is, If am not wrong, networkTimeout is in JDBC spec and I need not worry about what underlying DB I use and I need not change any code even if I move from one DB to another.

@harawata

This comment has been minimized.

Copy link
Member

commented May 6, 2019

Thank you for the explanation, @giridharkannan !
A new property defaultNetworkTimeout has been added to PooledDataSource and UnpooledDataSource (see #1538 ).
It would be greatly appreciated if you could try the latest 3.5.2-SNAPSHOT and check it with your solution.

@giridharkannan

This comment has been minimized.

Copy link
Author

commented May 7, 2019

Thanks for your quick fix. I have checked the code and it seems good.

Since most of the people use SqlSessionFactory and have no direct access to PooledDataSource or UnpooledDataSource we can't use the newly added method.

It would be great if this property could be set via Configuration or via some environment variable.

@harawata

This comment has been minimized.

Copy link
Member

commented May 8, 2019

Given that you want the same timeout value for all connections, calling setNetworkTimeout() every time SqlSession is opened seems inefficient.
It would be more appropriate to do that in a layer closer to the driver e.g. connection pool.

@giridharkannan

This comment has been minimized.

Copy link
Author

commented May 9, 2019

Let me rephrase my question.

How would I set the defaultNetworkTimeout which is available in Pooled and Unpooled DataSource class and end up using the same DataSource instance in SqlSessionFactory?

@harawata

This comment has been minimized.

Copy link
Member

commented May 10, 2019

With XML config, you can specify defaultNetworkTimeout as a property to POOLED or UNPOOLED data source.

<dataSource type="UNPOOLED">
  ...
  <property name="defaultNetworkTimeout" value="10000"/>
</dataSource>

Then pass the config file to SqlSessionFactory#build().

 try (Reader reader = Resources.getResourceAsReader("path/to/mybatis-config.xml")) { 
   sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); 
 } 

With Java code:

Configuration configuration = new Configuration(); 
UnpooledDataSource ds = new UnpooledDataSource(...);
ds.setDefaultNetworkTimeout(10000);
Environment environment = new Environment(..., ds); 
configuration.setEnvironment(environment); 
// ... add mappers, etc.
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); 

Does this answer your question?
Please let me know if I misunderstood!

@giridharkannan

This comment has been minimized.

Copy link
Author

commented May 10, 2019

Thanks for your quick answer. It did answer my question and am sorry for making you spoon feed me.

Really appreciate your time and effort.

@kazuki43zoo kazuki43zoo changed the title Ability to set networkTimeout - Feature Support networkTimeout on built-in DataSource classes Jul 3, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.