2010-09-27

Example: how to start EclipseLink (or other JPA) transaction declaratively in Spring

Here is example of declarative transaction management with EclipseLink JPA provider in Spring-based application. Similar approach should work with other JPA providers as well.
I use Maven for building so these were the required dependencies:


org.eclipse.persistence
javax.persistence
2.0.0


org.aspectj
aspectjrt
1.6.9


org.springframework
spring-aspects
3.0.4.RELEASE



Here is Spring's application context file:



xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">





















Now inside of your.services.package you can have such class:


@Component //instantiate as Spring's bean
public class ProductServiceImpl implements ProductService {
//Trick part #1 - Spring substitutes here entity manager
//and handles it in a way that each thread thinks he's the only one working with this manager.
//For E.M. marked by this annotation declarative transaction management is possible
@PersistenceContext(type = PersistenceContextType.TRANSACTION)
private EntityManager containerEm;

//Trick part #2 - Spring wraps this method in transaction which is handled by containerEm
@Transactional(readOnly=false, propagation=Propagation.REQUIRED)
public void yourMethod() {
...
//Here you can do whatever you want with E.M.:
containerEm.createNativeQuery(...);
//or
em.persist(...);
//etc.
...
}//end of transactional method

}

2010-08-11

DataSource through JNDI in Tomcat: place for JDBC driver

Recently I needed to reconfigure DataSource in Tomcat 6 though JNDI. Previously it was configured in Spring application context, but there was need to use DataSource also in BIRT template where Spring configuration was of little use, so the only option was to use JNDI.

Project uses Maven to build itself and appropriate database driver (for MySQL) was included as dependency, so it was in a classpath. After configuring JNDI in Tomcat I've got an error. After some debug I found out that it couldn't find database driver - though correct jar was in WEB-INF/lib. The trick is that Tomcat's JNDI picks only jars which are in classpath of Tomcat itself - so jar should be placed in $TOMCAT_HOME/lib.

There is also an option to use simple-jndi which can run inside web-application and use jars from WEB-INF/lib, but it needs to set some system properties so that simple-jndi would be used instead of Tomcat's JNDI implementation. Does anybody know some other way to use JDBC driver with JNDI without placing it into Tomcat's lib folder?

2010-03-25

Blogger : value of data:postLabelsLabel

how to change the value of <data:postLabelsLabel/> ?

There are 2 ways for changing tag label in Blogger:
  • remove tag in template and put hardcoded label
  • preferred way

2010-02-03

Eclipse debugging error

on Eclipse debugging has the error message .. wtf ?


ERROR: JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510)
JDWP exit error AGENT_ERROR_TRANSPORT_INIT(197): No transports initialized [../../../src/share/back/debugInit.c:690]
FATAL ERROR in native method: JDWP No transports initialized, jvmtiError=AGENT_ERROR_TRANSPORT_INIT(197)


after googling - solution has been found.
Firewall should be reconfigured.

2009-11-23

Maven: adding profile-dependent resources to project

Recently I needed to use different .properties files in my project depending on whether it runs in IDE at my workstation or is packed as .war file and deployed on production server. Project uses Maven to build itself. I've made some notes about solving this problem which hopefully will help somebody to do something similar.

Profiles can be configured in pom.xml file and then activated either from command line (using -P key)
or by some condition - operating system, environment variables, enabled by default etc.
Notice that few profiles can be activated at the same time (in my application this feature isn't used, but it was the reason why I didn't enable some profile by default).
Profiles can define plugins, properties, modules, resources and lot of other things which are enabled only for specific profile. But in this post we will need only resources settings. Detailed information about maven profiles available in Maven: The Definitive Guide and at Apache Maven Site.

So, suppose we have 3 different directories with main (meaning non-test) resources in our Maven-controlled project:
  • our_project/src/main/resources - common resources
  • our_project/src/main/resources_development - resources only for development version
  • our_project/src/main/resources_production - resources only for production version
Such resources may be .properties-files with data source or logger configuration or something like that. Files from resources directory (for all profiles) and files either from resources_development or resources_production (depending on selected profile) should be placed to resulting web-archive (.war), and, while making their way to archive, to our_project/target/classes (for example, from here Eclipse IDE deploys resource files to local Tomcat) and to our_project/target/our_war_file_name/WEB-INF/classes (our_war_file_name actually replaced by configured name of resulting .WAR file, from here Maven builds .WAR file).

In order to achieve this profiles may be configured this way in pom.xml file:



production



${basedir}/src/main/resources_production


${basedir}/src/main/resources





develop



${basedir}/src/main/resources_development


${basedir}/src/main/resources





This code defines 2 profiles - "production" and "develop". Notice that profile-specific directory goes first - profile-specific resources will be copied first, and if common resources directory contains same files they will NOT be owerwritten.


How these profiles can be used: type mvn clean package -Pproduction to build .WAR file for production environment or mvn clean package -Pproduction to build .WAR file for development environment. Similar options may be configured in Eclipse "Run Configurations" dialogue. Note that if you run production build in your IDE then IDE may deploy production-specific resources on your development environment, so you will need to build development version to get things back. Goal "clean" for our case is really neede only if you build for one profile, and then build for another profile in the same directory - then some resouces may remain from previous profile. If you always build for the same profile in one directory "clean" can be skiped if you want to speed up build.

If you need more advanced resource manipulation you can see options of Maven resource plugin. Its basic configuration in pom.xml looks like this:




maven-resources-plugin
2.4.1

..some configuration...



..execution-specific settings. For example, some settings may be applied to different
phases.




Hopefully, it will help somebody :)