I have 2 situations.
hibernate.cfg:
<property name="connection.url">jdbc:log4jdbc:h2:./H2/test</property>
<property name="connection.driver_class">net.sf.log4jdbc.sql.jdbcapi.DriverSpy</property>
<property name="connection.username">user</property>
<property name="connection.password">password</property>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
H2 db is in my project near my src folder. If I try to test connection from simple class it works. It connects to the db that is in the right place in project folder.
Now I'm making new configuration to launch tomcat + servlets. All is fine, but tomcat creates new H2 database in his folder: (apache-tomcat-8.0.17\bin\H2\test.mv.db). Is it possible to make tomcat to take (to use) my test.mv.db from my project's folder ?
If I put my test.mv.db manualy from project into tomcat's dir - it is working (I'm not using "sa" with blank password). I don't want to place h2 db into user's folder like jdbc:h2:~/test).
Thank you.
You should append the hibernate prefix to all your properties.
Try giving H2 an absolute path to your database folder:
<property name="hibernate.connection.url">jdbc:h2:file:/your-project-path/test-db</property>
I don't think the Driver class setting is correct either. It should be:
<property name="hibernate.connection.driver_class" value="org.h2.Driver" />
Related
I have a Spring Project where I am using bean configuration file
beans.xml.Inside the bean Configuration file, i have defined some properties for a PlaceHolder which refers to classPath...While the application is running, the properties are getting loaded from /unknownPath/Dev/Loc1/System.properties
Where
${BUS_ENV}=Dev
${LOCATION1}=Loc1
<bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:${BUS_ENV}/${LOCATION1}/system.properties</value>
<value>classpath:${BUS_ENV}/lbsprocessor.properties</value>
</list>
</property>
<!-- Force system properties to override any deployed runtime properties -->
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
I didn't specify classpath while running my project in IDE
I don't have those files in my resource folder
There are around 65 such files exists(for various reasons) as Dev/Loc1/System.properties
I am not able to find from which location the properties are getting referred. Even after debugging, I couldn't find out what classpath refers to. Please help me with figuring out
If you are using eclipse IDE right click on your project select properties then select Java Build Path. On first tab Source there is one input named Default Output folder that value is your classpath. Check all your properties files are there in that path.
Referring to your point 2 problems might be in these line
<value>classpath:${BUS_ENV}/${LOCATION1}/system.properties</value>
<value>classpath:${BUS_ENV}/lbsprocessor.properties</value>
You are using classpath for file location which means these properties file have to be in the .war file at /Dev/Loc1/System.properties
If properties files are outside of project may be at system level you can access them like this
<value>file:${BUS_ENV}/${LOCATION1}/system.properties</value>
<value>file:${BUS_ENV}/lbsprocessor.properties</value>
eg:
<value>file:/home/testuser/system.properties</value>
I am using Mac OS ,in this we are storing the Configurations as a jar file under
/Library/Java/Extension.So java is directly referring classpath to that location by default.
I have found many posts on here about using Spring to externalize the Hibernate settings so it's not in the hibernate.cfg.xml config file. However, my question revolves around having a development environment that uses Eclipse and then deploying the generated WAR file out on a separate server.
I am currently using Eclipse Luna and deploying my Spring MVC web application to a local install of Tomcat 8. The hibernate.cfg.xml file currently resides in the resources folder. When I deploy it locally, it works great. However, the config uses my personal login credentials, which isn't good to have on the server for everyone to see.
What I need is to figure out a design to have the application work locally and when I deploy on the server.
I was thinking about creating a folder called conf and placing the hibernate.cfg.xml file in here and through the Eclipse classpath (Debug As configuration...) define it as a folder. That should allow the local version to still read in the proper files and work. But I am stuck on how to setup the server side of this. Since we just drop in the WAR file, it will auto deploy. I can use a PropertyPlaceholderConfigurer, but that will throw errors on my local copy.
The only other thing I can think of is to create a process where I create the folder for the application, unpack the WAR file into this folder each time minus the conf folder. And only update the conf folder on the server.
Is this a good design or is there a better way? I appreciate all the help and guidance on this!
I got this to work and figured I would share how I did it
I went ahead and created the "conf" folder in the root folder. In here I placed a hibernate.cfg.xml file, log4j.properties and messages.properties file. I then created a subfolder called "db" and placed db.properties file in there.
So the structure is:
ApplicationName
--> conf
--> db
db.properties
hibernate.cfg.xml
log4j.properties
messages.properties
db.properties
In this file, I placed the hibernate connection information (and other information)
db.driver=oracle.jdbc.driver.OracleDriver
db.dialect=org.hibernate.dialect.Oracle10gDialect
db.default_schema=<schema>
hibernate.session_context=thread
hibernate.max_fetch_depth=20
hibernate.show_sql=false
hibernate.format_sql=true
hibernate.use_sql_comments=false
hibernate.generate_statistics=false
hibernate.c3p0.min_size=1
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=3000
hibernate.c3p0.max_statements=50
hibernate.c3p0.idle_test_period=300
hibernate.c3p0.preferredTestQuery=SELECT 1 from DUAL
# DEV
db.url=jdbc:oracle:thin:#<url>
# Need to be changed
db.username=<username>
db.password=<password>
The hibernate.cfg.xml file just had the Entity mappings in it
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Entity mapping -->
<mapping class="com.mine.Entity1"></mapping>
<mapping class="com.mine.Entity2"></mapping>
</session-factory>
</hibernate-configuration>
In the HibernateUtil.java file, I added the following lines:
Configuration configuration = new Configuration();
configuration.configure();
PropertyResourceBundle prop = (PropertyResourceBundle) ResourceBundle.getBundle("db/db");
// Basic connection information
configuration.setProperty("hibernate.connection.username", prop.getString("db.username"));
configuration.setProperty("hibernate.connection.password", prop.getString("db.password"));
configuration.setProperty("hibernate.connection.url", prop.getString("db.url"));
configuration.setProperty("hibernate.connection.driver_class", prop.getString("db.driver"));
configuration.setProperty("hibernate.dialect", prop.getString("db.dialect"));
configuration.setProperty("hibernate.default_schema", prop.getString("db.default_schema"));
configuration.setProperty("hibernate.current_session_context_class", prop.getString("hibernate.session_context"));
// Handling SQL statements in the logs
configuration.setProperty("show_sql", prop.getString("hibernate.show_sql"));
configuration.setProperty("format_sql", prop.getString("hibernate.format_sql"));
configuration.setProperty("use_sql_comments", prop.getString("hibernate.use_sql_comments"));
// C3P0 Settings
configuration.setProperty("hibernate.c3p0.min_size", prop.getString("hibernate.c3p0.min_size"));
configuration.setProperty("hibernate.c3p0.max_size", prop.getString("hibernate.c3p0.max_size"));
configuration.setProperty("hibernate.c3p0.timeout", prop.getString("hibernate.c3p0.timeout"));
configuration.setProperty("hibernate.c3p0.max_statements", prop.getString("hibernate.c3p0.max_statements"));
configuration.setProperty("hibernate.c3p0.idle_test_period", prop.getString("hibernate.c3p0.idle_test_period"));
configuration.setProperty("hibernate.c3p0.preferredTestQuery", prop.getString("hibernate.c3p0.preferredTestQuery"));
In the *-servlet.xml file:
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db/db.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="false"/>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="cacheSeconds" value="60"/>
</bean>
In Eclipse, when I go to run the application, I must first go to "Debug As... => Debug Configuration...". Under Classpath, I add the conf folder under the user entries section.
That's it for Eclipse. When it runs, it will pull in the configs from the conf folder and work properly.
TOMCAT
To have this run on the server, I first created a folder to store the contents of conf. I created the folder /app/properties. I checked out the project from SVN and copied the contents of the conf folder into the newly created folder.
Lastly, I went into $TOMCAT_HOME/conf/catalina.properties and added the new folder into the classpath by adding it to the shared.loader property.
That's it! When the WAR is deployed, it pulls the information from the /app/properties location and the application works.
Any more information needed, please let me know!
Here is a way to do that. First you need to use JNDI to access the datasource. There are some examples how to that and you can probably find more.
One of the thing that need to be done is to configure datasource as resource in tomcat context. And the trick is to have different context configurations for your local deployment from eclipse and on production.
In local deployment pack context.xml as part of the war in META-INF folder but in production deployment create context.xml in $CATALINA_BASE/conf/[enginename]/[hostname]/ which would override context.xml supplied in war.
Another option is to use PropertyPlaceholderConfigurer with option to override settings from environment variables. This will require that you specify database credentials in environment variables in tomcat server launch configuration.
I'm trying to get all files within a directory structure of 3 levels deep.
For example:
- the image a.jpg exists in a folder /images/12/34/
- the image b.jpg exists in a folder /images/56/78
I've tried the outbound-gateway like stated in :
https://github.com/spring-projects/spring-integration-samples/blob/master/basic/ftp/src/test/resources/META-INF/spring/integration/FtpOutboundGatewaySample-context.xml and http://forum.spring.io/forum/spring-projects/integration/104612-inbound-ftp-polling-sub-directories?p=604430#post604430
My configuration :
<bean id="ftpSessionFactory"
class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
<property name="host" value="127.0.0.1"/>
<property name="port" value="21"/>
<property name="username" value="Administrator"/>
<property name="password" value="SgtSpeedy1"/>
<property name="fileType" value="2"/>
<property name="clientMode" value="2" />
</bean>
<int-ftp:outbound-gateway id="gatewayLS"
cache-sessions="false"
session-factory="ftpSessionFactory"
request-channel="inbound"
command="ls"
command-options=""
expression="'/images/*/*'"
reply-channel="toSplitter"/>
<int:channel id="toSplitter" />
<int-stream:stdout-channel-adapter channel="toSplitter" append-newline="true"/>
I've omitted the splitter and just print everything out for testing purposes.
When testing, I do not get any file.
I've tried setting the folder to /images/* and then it returns all images under the 'images' folder but not recursively as stated in the links provided. So folders /12/34 and /56/78 aren't taken into account.
I cannot see what I'm missing. Can anyone help?
P.S. I'm working on Spring Integration 2.2.6 without the option to upgrade to 4.0.2 (newest), because I'm using a framework. Otherwise I'd use the -R option for the gateway!
I just tested with foo/foo/bar/qux.txt and foo/foo/baz/fiz.txt with
<int-ftp:outbound-gateway id="gatewayLS"
session-factory="ftpSessionFactory"
request-channel="inbound"
command="ls"
command-options="-1"
expression="'foo/*/*'"
reply-channel="toSplitter"/>
and it worked fine; as expected...
11:34:55.983 DEBUG [main] ...[Payload ArrayList content=[fiz.txt, qux.txt]]...
(I added the -1 option to just get the filename).
This was using linux ftpd on the server side.
Are you sure the files are in /images and not images? (Or is the user chrooted so / is his home)?
I'm using Spring in a Console java application. I'm using PropertyPlaceholderConfigurer to load database details, and it works flawlessly:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:/appdata/configs/myapplication/connection.properties" />
</bean>
So the root dir will be relative to where the jar is located. I need the same functionality with log4j.properties
log4j.appender.file.File=/appdata/configs/myapplication/myapplication-log.log
How can I achieve this without defining environment variables?
Just tried it like this:
log4j.appender.file.File=\\appdata\\configs\\myapplication\\myapplication-log.log
And it works fine.
I am using EclipseLink as JPA implementation and I am adding these properties in the persistence.xml but I can't see any scripts generated? Where are they supposed to be saved or have I misunderstood this property.
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="both" />
Is it possible to define a script as well that would be run after the tables are created? The same way as a seed script in Rails?
EclipseLink should use "createDDL.jdbc" and place it in the current working directory by default. You can change this by specifying the "eclipselink.create-ddl-jdbc-file-name" property for the file name and "eclipselink.application-location" to change the location.