Changing location of c3p0.properties and hibernate.properties - java

I have a problem regarding the use of hibernate.properties and c3p0.properties. I am dependent upon an API. The API uses Hibernate. As such the API has it's own hibernate.properties and c3p0.properties file included within its delivery jar. I need to ensure I ignore these properties completely when instantiating my Hibernate SessionFactory. I do need these files to remain available on the CLASSPATH so that the API I depend upon can be configured. I simply need a way to instantiate my org.hibernate.SessionFactory so that it doesn't look at these files in any way. I don't mind using an xml configuration or setting my Hibernate properties manually. I just need to ensure that the properties in hibernate.properties and c3p0.properties are not read.

In order to do this you will have to use an XML hibernate config file. If both the hibernate.properties and hibernate.cfg.xml are present, the XML file will override the properties file.
You can find more information here:
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-xmlconfig
Note: your c3p0 properties can be defined in the same XML file.

Related

How can I override configuration in logback-spring.xml with properties in Spring's own application.properties?

In my Spring Boot project, I initially set up the logging configuration in the application.properties, e.g.
logging.file=/foo/bar/app.log
logging.file.max-size=1M
logging.file.max-history=10
logging.file.level.root=DEBUG
logging.file.level.root.com.my.app=DEBUG
logging.file.level.org.springframework.boot.diagnostics=DEBUG
logging.file.level.org.springframework.security=DEBUG
Let's say I have other environment-specific properties files named following this pattern application-xxx.properties with xxx being the Spring profile name passed as argument when running the app (via -Dspring.profiles.active=xxx).
In these files, I can override the log levels, e.g. for an application-qa.properties
logging.file.level.root=INFO
logging.file.level.root.com.my.app=INFO
logging.file.level.org.springframework.boot.diagnostics=INFO
logging.file.level.org.springframework.security=INFO
I can also override these in the command line with the same mechanism as explained above (-D ).
Now, I'm asked to transfer all this configuration into a single logback-spring.xml. Using this new style of configuration, is there a way to still override each properties?
Found these resources while looking for an answer:
neither official Spring doc: https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.logging
nor this DZone article mention that: https://dzone.com/articles/configuring-logback-with-spring-boot
this article on CodeJava: https://www.codejava.net/frameworks/spring-boot/logback-rolling-files-example even says it works the other way round: The configuration in XML file will override the logging properties in the application.properties file. !!!

Hibernate Properties or configuration file to use

For hibernate project what is recommended to use hibernate.properties file or hibernate.cfg.xml file.
What to use??
And please can u also specify why to use properties instead configuration and vice versa
which use is best hibernate.properties file OR hibernate.cfg.xml file
The hibernate.properties is the old way to represent the connection information, for newest versions (2.0+), it has been replaced by the XML-based configuration file, since it is more flexible and has more features.
However, there's a thread explaining how to combine them both, you should have a look at this thread: How to use hibernate.properties file instead of hibernate.cfg.xml.
You should also have a look to the Hibernate documentation http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html, chapter 3.7. XML configuration file.

How to use multiple configuration files for Log4j2?

I am writing Java code that tests a Java library. The library includes its own Log4j2 configuration as part of the distribution.
I would like to use Log4j2 in my test code without modifying the library's configuration.
Is there a way to have a separate Log4j2 configuration for my test code?
This is all running as command-line Java, no servers or web involvement at all.
EDIT
What I want is to be able to configure loggers, appenders, etc for the test code to use, and at the same time have the library code use its own separate configuration file for its logging.
The idea is to use Log4j2 in my test code, but without having to change the library's configuration file. Since the library configuration file is part of the library's distribution, I don't want to change it for testing.
This may be helpful:
Log4j2 will first look for log4j2-test.xml in the classpath
if that file is not found, it will look for log4j2.xml in the classpath
So one option is to copy the library's configuration (log4j2.xml) to log4j2-test.xml and add your own configuration to log4j2-test.xml.
Furthermore, Log4j2 supports XInclude in XML configuration, so you could use that feature to avoid duplicating the library's configuration in your log4j2-test.xml.
Log4j2 supports "Composite Configuration" which exactly matches your requirement. All you need to do is provide path to multiple files in log4j.configurationFile property. This can be passed from command line or added to log4j2.component.properties file in your application.
References
https://logging.apache.org/log4j/2.x/manual/configuration.html#CompositeConfiguration
https://logging.apache.org/log4j/2.x/manual/configuration.html#SystemProperties
There are two step you can try to solve for your issue
Create your own configuration file with your custom name(eg: xyz.properties/.xml)
You must add the following line to your java runtime command
cmd> java -Dlog4j.configuration=location/xyz.properties
If you use diffent name for configuration rather log4j.properties/.xml file you need to configure that file at runtime by above command for more info have a look here..
Correct format for using an alternate XML file to log4j2.xml:
java -Dlog4j.configurationFile=./location/log4j2-custom.xml
Assuming ./location/log4j2-custom.xml exists and is the new XML to replace log4j2.xml in this run
See:
https://github.com/kamalcph/Log4j2Examples/blob/master/src/main/java/in/co/nmsworks/log4j2/examples/CompositeConfigurationExample.java
Referencing https://logging.apache.org/log4j/2.x/manual/configuration.html states that you can add multiple comma separated files under log4j2.configurationFile property.
To use multiple configuration files, depending on the environment you must set the.
for example:
if (env.equals("DEV")) {
setConfigFile("log4j2-dev.xml");
}
public static void setConfigFile(String logConfigFile) {
File file = new File(logConfigFile);
LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
context.setConfigLocation(file.toURI());
}
first configure application.yaml file
spring:
profiles:
active: dev
---
spring:
message: running in the dev profile //just to output the message in the log
profiles: dev
logging.config: classpath:log4j2-dev.xml
---
spring:
profiles: prod
logging.config: classpath:log4j2-prod.xml
and create these similar files in your classpath
* log4j2-dev.xml
* log4j2-prod.xml

Hibernate doesn't log anything anymore, since I relocated the log4j.xml and hibernate.properties files

I had the following working organization
src/main/resources/log4j.xml
src/main/resources/hibernate.properties
I wanted to reorganize my webapp as follow:
src/main/resources/log/log4j.xml
src/main/resources/orm/hibernate.properties
The Logger.info("foobar") still logs well (after having set the log4jConfigLocation context parameter), and the app still has a working database connection.
The problem is that Hibernate doesn't log anything anymore, even if hibernate.show_sql is set to true. Any idea why? Should specify the new path to the log4j.xml file to Hibernate? If yes, how?
You could run your server with this VM argument:-
-Dlog4j.configuration=/path/to/log4j.xml
I usually tend to place the log4j.xml at the recommended default location unless there's a need to do otherwise... "convention over configuration" is important especially if other peers may be working on the same project in the future.
Log4j first looks for its configuration by looking at the system property "log4j.configuration". If that system property is not set, it looks for a log4j.properties or a log4j.xml file on the classpath.
So, if you really want the log4j.xml at src/main/resources/log/log4j.xml, you will have to set the log4j configuration system property. This is basically what limc does by supplying it as a vm argument.
Also like limc says, you should probably just keep the log4j.xml at the default location.

Can I Specify hibernate annotated classes in hibernate.properties?

Most or all the core hibernate configuration properties can be specified in a startup properties file, as an alternative to specifying mappings in hibernate.cfg.xml.
Is there an easy way to specify mappings of annotated classes in a properties file?
You can map annotated classes like this in the hibernate.cfg.xml configuration:
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
...
<mapping class="com.annotated.classes.EntityOne"/>
</session-factory>
Similar configuration can be written using a properties file i.e. hibernate.properties
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.mapping = ???
What I haven't figured out is how to specify the mapping of annotated classes (entities) using the properties configuration, if this is possible.
I don't think there is any way to specify entities in the properties file.
I suppose you could create your own custom initialization code using Configuration to accomplish what you are looking for. The problem with using a properties file, though, is that you cannot specify a property with a list of values unless you identify a way to split the value into a list. You would then need to write code that parses the value accordingly.

Categories