0

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

0

3 Answers 3

2

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.

5
  • 1
    My project is not a library. When I release it, QA may start testing it. But, the hibernate-c3p0 dependency is not in the assembly since I don't want to force them to use c3p0 as connection pooling framework. How does it help if I declare the dependency to c3p0 with the optional scope?
    – manash
    Mar 8, 2012 at 15:50
  • In this scenario, it doesn't. It's more useful for libraries and frameworks like Hibernate. in your case, QA must know themselves how to set up Hibernate correctly. Mar 8, 2012 at 15:59
  • But, since Hibernate may need the hibernate-c3p0 JAR if QA chooses to use C3P0 connection pooling, how does it work concerning the classpath? If I don't declare the C3P0 dependency in the POM, the JAR would not be on the classpath.
    – manash
    Mar 8, 2012 at 18:16
  • 1
    If you deploy it in a server, you can put it in the server´s classpath folder, just like you do with the JDBC driver. If you run it standalone, it will be more tricky to manipulate the classpath at runtime. Mar 8, 2012 at 18:26
  • @MickaelMarrache: See my edits how to improve the situation for QA. Mar 9, 2012 at 8:33
1

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.

4
  • If the dependency is not required for compilation and you will delegate to the container the responsibility to provide it, there is no reason to declare the dependency in your pom, not even as optional Mar 8, 2012 at 13:34
  • @Fabrice: true but for testing and development purposes it is advised to do this.
    – tom
    Mar 8, 2012 at 14:17
  • 1
    +1 "optional" means that your code can make use of this which in turn makes it easier for consumers of your project to configure their POMs correctly. Mar 8, 2012 at 14:39
  • I don´t see why it is advised. I use optional when I depend on a lib that doesn´t necessarily need to be imported by projects that depend on me. This is not Mickael scenario. Mar 8, 2012 at 18:01
0

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?

3
  • This solution doesn't help me since the project has to be recompiled (in order to refilter the properties file and to add the required runtime dependency), in the case production decides to use another connection pooling framework. Thanks anyway.
    – manash
    Mar 8, 2012 at 12:51
  • What kind of packaging are you using? JAR, WAR...? Mar 8, 2012 at 12:59
  • JAR and WAR. It concerns database access, I have stand alone and web apps that access the database.
    – manash
    Mar 8, 2012 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.