Runtime dependency (e.g. connection pooling) and classpath?

陌路散爱 提交于 2019-12-11 17:46:06

问题


I have a Maven 3 project that uses Hibernate 3. In the Hibernate properties file, there is an entry for hibernate.connection.provider_class with the class corresponding to the C3P0 connection provider (org.hibernate.connection.C3P0ConnectionProvider). Obviously, this class is only used at runtime, so I don't need to add the corresponding dependency in my POM with the compile scope. Now, I want to give the possibility to use any connection pooling framework desired, so I also don't add a runtime dependency to the POM.

What is the best practice?

I thought about adding an entry to the classpath corresponding to the runtime dependency (in this case, hibernate-c3p0) when the application is run (for example, using the command line). But, I don't know if it's possible.

This is almost (maybe exactly) the same problem as with SLF4J. I don't know if Hibernate also uses the facade pattern for connection pooling.

Thanks


回答1:


Since your code doesn't depend on the connection pooling (neither the main code nor the tests need it), there is no point to mention the dependency anywhere.

If anyone should mention it, then that would be Hibernate because Hibernate offers this feature in its config.

But you can add it to your POM with optional: true to indicate:

  1. I support this feature
  2. If you use it, then I recommend this framework and this version

That will make life slightly more simple for consumers of your project.

But overall, you should not mention features provided/needed by other projects unless they have some impact on your code (like when you offer a more simple way to configure connection pooling for Hibernate).

[EDIT] Your main concern is probably how to configure the project for QA. The technical term for this new movement is "DevOps" - instead of producing a dump WAR which the customer (QA) has to configure painstakingly, configuration is part of the development process just like everything else. What you pass on is a completely configured, ready-to-run setup.

To implement this, create another Maven module called "project-qa" which depends on your project and everything else you need to turn the dead code into a running application (so it will depend on DBCP plus it will contain all the necessary config files).

Maven supports overlayed WARs which will allow you to implement this painlessly.




回答2:


You can mark your dependency as optional. In this case it will not be packaged into archives. In this case you have to ensure that your container provides required library.




回答3:


You could use a different profile for each connection provider. In each profile you put the runtime dependency that correspond to the connection provider you want to use and change the hibernate.connection.provider_class property accordingly.

For more details about how to configure dependencies in profiles, see Different dependencies for different build profiles in maven.

To see how to change the value of the hibernate.connection.provider_class property see How can I change a .properties file in maven depending on my profile?



来源:https://stackoverflow.com/questions/9586567/runtime-dependency-e-g-connection-pooling-and-classpath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!