Spring 3, REST and transactions

Posted by – 23.03.2011

Today I had difficulties to get transactions with a REST service ie. spring mvc working.

The problem was that no persitence call on the entitymanager resulted in a phyical saved entity in the database.
My DAO classes were marked with @Repository and @Transactional Annotations. I was confused because the same DAO class was used in an other project with JSF bindings – without any problems.

The only different of the REST project and the JSF project are the dispatchers that are defined in the web.xml.

The JSF project defined the javax.faces.webapp.FacesServlet

<servlet>
	<servlet-name>Faces Servlet</servlet-name>
	<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
	<load-on-startup>1</load-on-startup>
</servlet>

In the REST project with the described problem there is defined:

<servlet>
	<servlet-name>rest</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<load-on-startup>1</load-on-startup>
</servlet>

The difference here is that the DispatcherServlet is looking for a [servlet-name]-servlet.xml configuration file. So I defined my REST related suff there.
Also I defined the following in the web.xml

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>
			/WEB-INF/rest-context.xml
	</param-value>
</context-param>

In the rest-context.xml I put all my entity and datasource related suff.

The interesting point is, that by using this two different configuration files, spring does’t recognise the transaction context. I’m not sure if there is a second context or if it’s just one not working.

The solution to this confusion is to put all configuration from the [servlet-name]-servlet.xml into the servlet-context.xml file.

That’s it!

If anyone can point me out why those two configuration files are not interpreted and working together, you’re welcome to comment! :-)

Cheers

spring-webmvc, hibernate jpa and lazy loading

Posted by – 21.03.2011

Hi again,

another thing I found while using spring and other web technologies:

When you try to display entities which are connected to other entities in a web view you should consider to use eager loading fetch strategies.

If you want to fetch depending entities lazy you most probably will see a  LazyInitializationException (LIE).

A workaround is to use the OpenSessionInView (OSIV) Pattern to let spring initialize a valid session when rendering the view. To do this with spring configured with JPA add the following to your web.xml:

        <filter>
		<filter-name>OpenEntityManagerInViewFilter</filter-name>
		<filter-class>
			org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
		<init-param>
			<param-name>entityManagerFactoryBeanName</param-name>
			<param-value>entityManagerFactory</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>OpenEntityManagerInViewFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

mySQL and spring-security 3

Posted by – 21.03.2011

Hi,

I just stumbled over a mySQL feature when developing on Windows and deploying on Linux.

Be aware of the fact that mySQL on Linux is case-sensitive! On Windows it is not!

What I did:

Wrote a jdbc query to get the authentication credentials for spring security:

<authentication-manager alias="authenticationManager">
		 <authentication-provider>

			<jdbc-user-service
			data-source-ref="dataSource"
			users-by-username-query="SELECT username, password, active as enabled FROM User WHERE username = ?"
			authorities-by-username-query="SELECT username, authority FROM Role JOIN User ON Role.id = User.role_id AND User.username = ?"

			/>

		</authentication-provider>
	</authentication-manager>

As you can see I wrote the table names with a capital letter so that mySQL on Linux can find them!

Strange was, that I didn’t get any error message in the logs.

Under Construction

Posted by – 05.03.2011

I’m currently moving the domain to a new server.

I just couldn’t hold that damn slow webhosting package. It’s too slow, bad to manage and has a bad overall performance. Now I’m back on my own root server and starting to breath again. :-)

Coming back soon!

 

PS: Never ever think of hosting your site on STRATO!