Spring JMS and JPA XA Transactions with WebLogic


This article shows the configuration of using XA transactions with the resources managed by WebLogic server.

There are three parts of configurations for this XA transactions to work - JMS, JPA and JTA. Both JMS and JPA are acquired through the JNDI registry and the XA transaction manager implementation uses the one from the WebLogic server. This configuration relies on WebLogic server. The concept should be similar and applicable to other JEE application servers. With these configurations, we could easily use @Transactional annotation on the services for XA transactions.

JmsConfiguration.java Excerpt

public class JmsConfiguration {
    // properties...

    public ConnectionFactory connectionFactory() {
        return lookupByJndiTemplate(jmsConnectionFactory, ConnectionFactory.class);

    public JndiTemplate jndiTemplate() {
        JndiTemplate jndiTemplate = new JndiTemplate();
        return jndiTemplate;

    public JmsTemplate jmsTemplate() throws Throwable {
        return new JmsTemplate(connectionFactory());

    // local methods...

Convient methods:

protected <T> T lookupByJndiTemplate(String jndiName, Class<T> requiredType) {

    try {
        Object located = jndiTemplate().lookup(jndiName);
        if (located == null) {
            throw new NameNotFoundException("JNDI object with [" + jndiName + "] not found");
        return (T) located;
    } catch (NamingException e) {
    return null;

private Properties getJndiProperties() {
    final Properties jndiProps = new Properties();
    jndiProps.setProperty(Context.INITIAL_CONTEXT_FACTORY, jmsInitContextFactory);
    jndiProps.setProperty(Context.PROVIDER_URL, jmsUrl);
    return jndiProps;

JpaConfiguration.java Excerpt

@EnableJpaRepositories(basePackages = "com.leafyjava.xa-demo-weblogic.repositories", entityManagerFactoryRef = "entityManager")
public class JpaConfiguration {
    // properties....

    private JmsConfiguration jmsConfiguration;

    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
        return hibernateJpaVendorAdapter;

    public DataSource dataSource() throws NamingException {

        return jmsConfiguration.lookupByJndiTemplate(jdbcJndi, DataSource.class);

    public LocalContainerEntityManagerFactoryBean entityManager() throws  Throwable {
        Properties properties = new Properties();
        properties.setProperty("hibernate.transaction.jta.platform", WeblogicJtaPlatform.class.getName());
        properties.setProperty("javax.persistence.transactionType", "JTA");

        LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();

        return entityManager;

JtaConfiguration.java Excerpt

public class JtaConfiguration {

    public PlatformTransactionManager transactionManager() throws Throwable {
        WebLogicJtaTransactionManager transactionManager = new WebLogicJtaTransactionManager();
        return transactionManager;


  1. Excellent work. This post that you have shared is attractive. Really, I was searching for these types of blogs. Thank you for sharing this blog. managed IT services Naples FL


Post a Comment

Popular posts from this blog

Creating a Library Project for Spring Boot

Spring Data Repository Query Precedence Tricks

Overwriting Spring Security Context through Filter