HSQLDB embedded in Spring MVC application with file-based persistence - spring

I am working with a Spring MVC web application that simulates an in-memory persistence layer using a combination of Maps, Lists, etc. Obviously, a database is much more performant, so I want to use HSQLDB. I want the database to be embedded from the standpoint of being accessible only via the application and starting/stopping with the web application. But I want the data to be permanent, i.e. file-based so even if the application is restarted, the data is loaded from a file. Is there a way to achieve this?
Note (added 4/28/2015): I want this to work in the following way:
Web application starts up
Web application checks presence of database (file) in configured location
If database is not available at configured location, it creates by running a provided SQL initialization script.
I added the following to my Spring configuration file:
<bean id="embeddedDataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:file:/usr/local/data/hsqldb/testdb" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
But I don't see any database file created at the location /usr/local/data/hsqldb/

Related

Spring Framework JDBC - Database password changes

Am running a Spring Application deployed in Apache Tomcat server
With below bean,
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/appln" />
<property name="username" value="root" />
<property name="password" value="password" />
<property name="initialSize" value="3" />
<property name="maxActive" value="10" />
</bean>
How does this bean works, Will this bean validates the DB credentials for every DB request made from application ?
When application is running, and the DataBase password is updated, will this bean fails for any application request or it works since it is already validated
For the first question, when Datasource object as bean(singleton) is created, the database information is validated the first time. After that when you inject your Datasource bean to the transaction manager bean, the database operations will be managed by these beans so open session, commit, rollback etc. Look the #Transactional annotation usage.
For the second one, you could change the bean definition in jar/war/ear, after that restart your application. But you can pass these information in the configuration file like application.properties/datasource.properties that you specified in applicationContext.xml. This will simplify your deployments when you change anythings. Without the restart, you cannot do to pass new password for database.
Adding to what Semih Okan Pehlivan said,
It is possible to refresh the password for the database if you put the properties in the application.properties, though you would need to add spring-cloud-starter to your dependencies.

Is it connected to Hibernate or not?

Lately I found an example of Spring Boot CRUD. In the read me there is written :
This project is based on the Spring Boot project and uses these
packages :
Maven
Spring Core
Spring Data (Hibernate & MySQL)
Spring MVC (Tomcat)
Thymleaf
In the source code I do not see anything that would look like this app is somehow connected to the hibernate. Could you help me to solve this little problem? And if it is not connected to the Hibernate how can I connect CRUD like that to the Hibernate?
Thanks for your help :)
In example you've provided you're using spring-boot-starter-data-jpa which already contains predefined hibernate dependencies (see pom.xml).
How to work with SQL databases described in documentation section.
Basically you configure hibernate using application.properties using following prefix:
spring.jpa.properties.hibernate.*
for Spring boot with hibernate you can follow bellow link :-
https://github.com/netgloo/spring-boot-samples
you have to configure hibernate property and datasource property for database connection... but for example i can share some code for Spring hibernate and JPA but Spring boot with hibernate you can follow link:-
<bean id="hibernateJpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.amstech.mayal.entity" />
<property name="jpaDialect" ref="hibernateJpaDialect" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.connection.driver_class" value="${database.jdbc.driver.class}" />
<entry key="hibernate.connection.url" value="${database.jdbc.url}" />
<entry key="hibernate.connection.username" value="${database.user}" />
<entry key="hibernate.connection.password" value="${database.password}" />
<entry key="hibernate.dialect" value="${hibernate.dialect}" />
<entry key="show_sql" value="true" />
<entry key="eclipselink.jdbc.exclusive-connection.is-lazy"
value="true" />
</map>
</property>
</bean>
I would suggest looking at the Spring Boot Data section of the main documentation. There is a lot less configuration that is needed and you can do it fluently and leave the xml behind. JPA + Hibernate is Spring data have become highly interlinked in boot.
There are multiple ways in which spring boot interact with hibernate. In the example you shared it is picking up the db properties from application.properties file and setting up the configuration. Rest of the things it will pick from the dependency provide in pom.xml.
Yes, it is connected with the hibernate. Things you need to do apart from setting up the project is to setting up a database with some username and password. And creating a db schema.Rest of the things will be done by spring boot. Make sure your db username password matches with the application file properties.

What is the difference : BasicDataSource and JOnASDataBaseManagerService

We have a web site using spring.
I found in the code 2 ways of connecting to the oracle database, both use a bean called phareDataSource :
1st method :
<bean id="phareDataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver" />
<property name="url"
value="${hibernate.connection.url}" />
<property name="username" value="${hibernate.connection.username}" />
<property name="password" value="${hibernate.connection.password}" />
</bean>
and 2nd method :
<bean id="phareDataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>jdbc/PHARE</value>
</property>
</bean>
In Jonas Directory : jonas.properties
jonas.service.dbm.class org.ow2.jonas.dbm.internal.JOnASDataBaseManagerService
jonas.service.dbm.datasources PHARERH
PHARERH.properties :
datasource.name jdbc/PHARE
datasource.url jdbc\:oracle\:thin\:#blabla\:1521\:R59A001
datasource.classname oracle.jdbc.driver.OracleDriver
datasource.username bla
datasource.password bla
The first method or the second one is picked when we are building (maven active profile).
The first uses a simple configuration file, the second uses a configuration file located in jonas conf directory.
Since we use tomcat in our dev environment, we picked the first method.
But in our int and prod environment with Jonas installed, shouldn't we use the second method ?
Is it better in performance ?
Both will use a JDBC connection pool to manage your connections so I would not expect there to be a major difference in performance.
Method 1 doesn't use any of the JOnAS features to create or manage the JDBC connections + pool. The same configuration will work outside of the application server. This is useful for testing without the requirement to deploy to the application server.
Method 2 will use the JDBC connection pool configured and managed by JOnAS (the application server).
Typically, if you have made the decision to go with an application container (e.g. JOnAS, JBoss, Websphere, etc) it is usually a good idea to let the container manage the resources that you use. That's what they are designed to do, and you may require some of the advanced features for managing the configured resources. There is also the benefit that your application doesn't have to know what implementation/driver is being used (or username/password, so you can deploy your EAR/WAR to different application servers without having to change your application configuration. These details are configured in the server instance instead.
If using Method 1 inside an application server, the server will have no control over the threads being created, because it knows nothing about them.

How do you use a scope Tomcat DataSource with Spring Batch?

I am currently using Spring Batch to import data from a SQL server. In order to make the datasource configurable I needed to "step scope" the datasource bean. However, this concerns me. If the datasource bean, which does connection pooling, is step scoped, then how can it manage connection in a pool and is there even a benefit to using it.
My datasource is configured as follows:
<bean id="dataSourceMssql" class="org.apache.tomcat.jdbc.pool.DataSource" scope="step">
<property name="driverClassName" value="${batch.mssql.driver}" />
<property name="username" value="${batch.mssql.user}" />
<property name="password" value="${batch.mssql.password}" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="3610" />
<property name="url"
value="${batch.mssql.connect}#{jobParameters['dburl']}:#{jobParameters['port']}/#{jobParameters['databaseName']}" />
</bean>
Why is it step scoped? Because I needed to retrieve the jobParameters to configure the datasource.
What do I want to know?
Will connection pooling still occur? (Perhaps the beans resources stay alive and are reclaimed)
I appreciate the help.
The scope "step" is only usable on spring batch beans. Other beans (Spring) only know the scope : singleton, prototype, request or session.
Normal way to handle this is to set these parameters in a properties file read by your applicatioonContext.xml.
JobParameters are used to pass Job related parameters (path, filename, date, seqNo etc) since a job with the same JobParameters won't be able to run twice.
EDIT: Is your job multithreaded? Because majority of the jobs created are single threaded! If your job is in fact single thread, i would ask myself why pooling connections is needed!
Regards

Spring disconnecting from Derby

I'm using Apache Derby with the Spring JdbcTemplate inside a web app running on Tomcat.
Spring is managing the data source. I've noticed that if I update the .war file and Tomcat undeploys/redeploys the app, I get this error:
java.sql.SQLException: Another instance of Derby may have already booted the database /tmp/manager_db/manager.
Restarting Tomcat fixes the problem, but as a purist, I'd like to clean things up properly when the webapp is undeployed.
The Embedded driver doesn't seem to have a 'close' method to put in the bean declaration under 'destroy-method'. I know the shutdown is normally achieved using a 'shutdown' connection URL, "jdbc:derby:;shutdown=true".
Any suggestions?
Here's the declaration in the Spring config file for my data source (the db won't be under /tmp/, just there for now).
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="url" value="jdbc:derby:/tmp/manager_db/manager;create=true"/>
<property name="username" value=""/>
<property name="password" value=""/>
</bean>
I think a better answer is to use the Tomcat JNDI data source pool with Spring. Spring's JDBC template will return the connection to pool when it's done.

Resources