Pokazywanie postów oznaczonych etykietą hibernate. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą hibernate. Pokaż wszystkie posty

wtorek, 16 stycznia 2018

Hibernate performance tips & tricks

Thanks to twitter I've recently found a very nice and comprehensive list of Hibernate performance tips & tricks, written by Vlad Mihalcea. It's available on his blog here: https://vladmihalcea.com/14-high-performance-java-persistence-tips/
Really recommend it! Even with years of experience with Hibernate as a persistence provider I managed to find something new :-) 

wtorek, 25 lipca 2017

[memo] hibernate's import.sql

There is a neat, heavily undocumented feature oh Hibernate - import.sql.

If Hibernate creates DB schema from scratch - "hibernate.hbm2ddl.auto" is set to either "create" or "create-drop", it will load "import.sql" file located in the root classpath. If Spring Boot is used, the property controlling the behaviour is "spring.jpa.hibernate.ddl-auto".

But why would anyone like to import some static data in a webapp? Well, for prototyping purposes for example. Or for "static" webapp mocking some external resources for integration testing. 

czwartek, 7 sierpnia 2008

HSQL: follow-up

It appears that sometimes one would like to use lightweight HSQL for unit / integration testing of his (hers) Spring-aware application. Nothing simpler ;-)
First thing is preparing Spring bean able to manage lifecycle of an instance of the HSQL database. This could be written by hand, but fortunately convenient class is already available in the Springmodules project. Bean implementation is to be found here.
Second thing is Spring XML configuration of the bean. My sample code is as follows:

<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="org.hsqldb.jdbcDriver"/>
<property name="url"
value="jdbc:hsqldb:hsql://localhost/test"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
<property name="defaultAutoCommit" value="true"/>
</bean>

<bean id="dataBase"
class="springmodules.HsqlServerBean"
singleton="true" lazy-init="false">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="serverProperties">
<props>
<prop key="server.port">9001</prop>
<prop key="server.database.0">
file:webapps/SampleApp/db/test</prop>
<prop key="server.dbname.0">test</prop>
</props>
</property>
</bean>

Configuration is pretty straight-forward. Note that we're using DataSource referring to the database configuration. This is done because:
a) Spring bean requires DataSource to commence db shutdown when container goes down
b) helps encapsulating database properties in one entity
The latter can be used more when Hibernate is also in the game. SessionFactory can be configured to use the DataSource with such code as:

<bean id="sessionFactoryBean" class=
"org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml"/>
<property name="dataSource" >
<ref bean="dataSource"/>
</property>
</bean>


Have fun!

piątek, 25 kwietnia 2008

Simple & fast DB testing

Yes, you're right. This short post is about HSQL - powerful yet surprisingly simple to use, pure-Java RDBMS.
Developing db-related functionalities in Java has never been easier. Using Hibernate's features as on-the-fly schema generation developer can easily stick to changing requirements. But what about testing? Transferring data to and from DB could take considerable amount of time and usage of one, shared, remote db can change test conditions and therefore make results unpredictable.
Solution is straight forward - configure own, isolated DB for all the testing! In Java world there could be only one answer - Hypersonic SQL (HSQL). In simplest example, one could use embedded HSQL running in-memory database.
Only two things are needed to enjoy separated DB test environment:
  • add HSQL to the test class path - using Maven2 it's as simple as adding dependency to project's POM:

    <dependency>
    <groupid>hsqldb</groupid>
    <artifactid>hsqldb</artifactid>
    <version>1.8.0.7</version>
    <scope>test</scope>
    </dependency>

  • configure and start org.hsqldb.Server class.
The latter can be done by writing simple wrapping class:

public class HsqlBootstrap
extends Thread{

private Server server;

public HsqlBootstrap() {
super();
server = new Server();
server.setNoSystemExit(true);

server.setDatabasePath(0, "mem");
server.setDatabaseName(0, "testDb");
}

public void run() {

server.start();
}

public void stopServer() {
server.stop();
}
}

There is of course need for configuring Hibernate to use our new, freshly created DB. Adequate properties could be found on various Hibernate-related sites and include using jdbc:hsqldb:mem:testDb connection string ;-)