Skip to content

Commit

Permalink
Merge pull request eugenp#127 from Doha2012/master
Browse files Browse the repository at this point in the history
fix jpa multiple db
  • Loading branch information
Eugen committed Jan 31, 2015
2 parents fff145e + 3cc4fb9 commit 54c49c7
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 93 deletions.
7 changes: 0 additions & 7 deletions spring-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,6 @@
<version>1.6.0.RELEASE</version>
<scope>compile</scope>
</dependency>

<!-- atomikos -->
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jta</artifactId>
<version>3.9.3</version>
</dependency>

<!-- validation -->

Expand Down

This file was deleted.

22 changes: 0 additions & 22 deletions spring-jpa/src/main/java/org/baeldung/config/MyJtaPlatform.java

This file was deleted.

13 changes: 10 additions & 3 deletions spring-jpa/src/main/java/org/baeldung/config/ProductConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import com.google.common.base.Preconditions;

@Configuration
@PropertySource({ "classpath:persistence-multiple-db.properties" })
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.multiple.dao.product", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "transactionManager")
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.multiple.dao.product", entityManagerFactoryRef = "productEntityManager", transactionManagerRef = "productTransactionManager")
public class ProductConfig {
@Autowired
private Environment env;
Expand All @@ -36,8 +38,6 @@ public LocalContainerEntityManagerFactoryBean productEntityManagerFactory() {
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
final HashMap<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.transaction.jta.platform", MyJtaPlatform.class.getName());
properties.put("javax.persistence.transactionType", "JTA");
properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
Expand All @@ -56,4 +56,11 @@ public DataSource productDataSource() {
return dataSource;
}

@Bean(name = "productTransactionManager")
public PlatformTransactionManager transactionManager() {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(productEntityManagerFactory().getObject());
return transactionManager;
}

}
15 changes: 12 additions & 3 deletions spring-jpa/src/main/java/org/baeldung/config/UserConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import com.google.common.base.Preconditions;

@Configuration
@PropertySource({ "classpath:persistence-multiple-db.properties" })
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.multiple.dao.user", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "transactionManager")
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.multiple.dao.user", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "userTransactionManager")
public class UserConfig {
@Autowired
private Environment env;
Expand All @@ -36,8 +39,6 @@ public LocalContainerEntityManagerFactoryBean userEntityManagerFactory() {
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
final HashMap<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.transaction.jta.platform", MyJtaPlatform.class.getName());
properties.put("javax.persistence.transactionType", "JTA");
properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
Expand All @@ -56,4 +57,12 @@ public DataSource userDataSource() {
return dataSource;
}

@Primary
@Bean(name = "userTransactionManager")
public PlatformTransactionManager transactionManager() {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(userEntityManagerFactory().getObject());
return transactionManager;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(schema = "spring_jpa_product")
public class Product {

@Id
Expand Down Expand Up @@ -41,4 +43,10 @@ public void setPrice(final double price) {
this.price = price;
}

@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Product [name=").append(name).append(", id=").append(id).append("]");
return builder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.baeldung.persistence.multiple.model.user;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(schema = "spring_jpa_user")
public class User {

@Id
Expand All @@ -14,6 +17,7 @@ public class User {

private String name;

@Column(unique = true, nullable = false)
private String email;

private int age;
Expand Down Expand Up @@ -54,4 +58,10 @@ public void setAge(final int age) {
this.age = age;
}

@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("User [name=").append(name).append(", id=").append(id).append("]");
return builder.toString();
}
}
11 changes: 11 additions & 0 deletions spring-jpa/src/main/resources/persistence-multiple-db.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# jdbc.X
jdbc.driverClassName=com.mysql.jdbc.Driver
user.jdbc.url=jdbc:mysql://localhost:3306/spring_jpa_user?createDatabaseIfNotExist=true
product.jdbc.url=jdbc:mysql://localhost:3306/spring_jpa_product?createDatabaseIfNotExist=true
jdbc.user=tutorialuser
jdbc.pass=tutorialmy5ql

# hibernate.X
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create-drop
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.baeldung.persistence.service;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import org.baeldung.config.MultipleDBJPAConfig;
import org.baeldung.config.ProductConfig;
import org.baeldung.config.UserConfig;
import org.baeldung.persistence.multiple.dao.product.ProductRepository;
Expand All @@ -12,15 +12,15 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { MultipleDBJPAConfig.class, UserConfig.class, ProductConfig.class })
@Transactional
@TransactionConfiguration(transactionManager = "transactionManager")
@ContextConfiguration(classes = { UserConfig.class, ProductConfig.class })
@TransactionConfiguration
public class JPAMultipleDBTest {
@Autowired
private UserRepository userRepository;
Expand All @@ -29,16 +29,41 @@ public class JPAMultipleDBTest {
private ProductRepository productRepository;

@Test
@Transactional("userTransactionManager")
public void whenCreatingUser_thenCreated() {
User user = new User();
user.setName("John");
user.setEmail("[email protected]");
user.setAge(20);
user = userRepository.save(user);

assertNotNull(userRepository.findOne(user.getId()));
}

@Test
@Transactional("userTransactionManager")
public void whenCreatingUsersWithSameEmail_thenRollback() {
User user1 = new User();
user1.setName("John");
user1.setEmail("[email protected]");
user1.setAge(20);
user1 = userRepository.save(user1);
assertNotNull(userRepository.findOne(user1.getId()));

User user2 = new User();
user2.setName("Tom");
user2.setEmail("[email protected]");
user2.setAge(10);
try {
user2 = userRepository.save(user2);
} catch (final DataIntegrityViolationException e) {
}

assertNull(userRepository.findOne(user2.getId()));
}

@Test
@Transactional("productTransactionManager")
public void whenCreatingProduct_thenCreated() {
Product product = new Product();
product.setName("Book");
Expand All @@ -48,4 +73,5 @@ public void whenCreatingProduct_thenCreated() {

assertNotNull(productRepository.findOne(product.getId()));
}

}

0 comments on commit 54c49c7

Please sign in to comment.