As a data source factory, DataSourceFactory does not follow the factory specifications. It has two issues:

  1. The factory can only produce one product. For example, calling getDataSource multiple times can only get the same DataSource.
  2. Modifications to factory attributes can affect products that have already been shipped. For example, calling the setProperties method after calling the getDataSource method to get a DataSource will affect the DataSource.

This submission fixes the above two issues.

Comment From: harawata

Thank you for the PR, @yeecode ,

It does not seem to make sense to return different DataSource instance on each getDataSource() call and it's not how MyBatis uses this interface. Could you explain why you need this change?

Comment From: yeecode

Yes, there is no difference in results. Because currently MyBatis only needs to get one DataSource.

But as a factory, multiple calls to return the same product is against the pattern. In other words, if multiple DataSources are needed in the future, the developer may call getDataSource () multiple times and then get the desired result.

So this commit is not used to fix errors, but to improve normativeness.

Comment From: harawata

Thank you for a reply, @yeecode !

IIRC, it's called 'singleton factory'. And, in the current implementation, we are supposed to create DataSourceFactory when we need a new DataSource instance, I think.

Comment From: yeecode

Based on this design, the factory is used to ensure the unity of the product. That current implementation is reasonable.