Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit 9dd6d25

Browse files
committed
Add SPR-12118
1 parent 10b59ef commit 9dd6d25

File tree

7 files changed

+503
-0
lines changed

7 files changed

+503
-0
lines changed

SPR-12118/README.md

+210
Large diffs are not rendered by default.

SPR-12118/pom.xml

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.springframework.issues</groupId>
5+
<artifactId>SPR-12118</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<packaging>war</packaging>
8+
<properties>
9+
<spring.version>4.0.6.RELEASE</spring.version>
10+
</properties>
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.springframework</groupId>
14+
<artifactId>spring-core</artifactId>
15+
<version>${spring.version}</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.springframework</groupId>
19+
<artifactId>spring-webmvc</artifactId>
20+
<version>${spring.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.springframework</groupId>
24+
<artifactId>spring-context</artifactId>
25+
<version>${spring.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework</groupId>
29+
<artifactId>spring-tx</artifactId>
30+
<version>${spring.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework</groupId>
34+
<artifactId>spring-jdbc</artifactId>
35+
<version>${spring.version}</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework</groupId>
39+
<artifactId>spring-orm</artifactId>
40+
<version>${spring.version}</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>javax.servlet</groupId>
44+
<artifactId>javax.servlet-api</artifactId>
45+
<version>3.1.0</version>
46+
<scope>provided</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.hibernate</groupId>
50+
<artifactId>hibernate-entitymanager</artifactId>
51+
<version>4.3.6.Final</version>
52+
</dependency>
53+
</dependencies>
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-compiler-plugin</artifactId>
59+
<version>3.1</version>
60+
<configuration>
61+
<source>1.7</source>
62+
<target>1.7</target>
63+
</configuration>
64+
</plugin>
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-war-plugin</artifactId>
68+
<version>2.4</version>
69+
<configuration>
70+
<failOnMissingWebXml>false</failOnMissingWebXml>
71+
</configuration>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.test.wildflytransactions;
18+
19+
import javax.persistence.Entity;
20+
import javax.persistence.GeneratedValue;
21+
import javax.persistence.Id;
22+
23+
@Entity
24+
public class Account {
25+
26+
@Id
27+
@GeneratedValue
28+
private Long id;
29+
30+
private String username;
31+
32+
Account() {
33+
}
34+
35+
public Account(String username) {
36+
this.username = username;
37+
}
38+
39+
public String getUsername() {
40+
return this.username;
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.test.wildflytransactions;
18+
19+
import javax.persistence.EntityManager;
20+
import javax.persistence.PersistenceContext;
21+
22+
import org.springframework.stereotype.Service;
23+
import org.springframework.transaction.annotation.Transactional;
24+
25+
@Service
26+
@Transactional
27+
public class AccountService {
28+
29+
@PersistenceContext
30+
private EntityManager em;
31+
32+
33+
public void createAccountAndNotify(String username) {
34+
Account entity = new Account(username);
35+
this.em.persist(entity);
36+
}
37+
38+
@Transactional(readOnly = true)
39+
public long getCount() {
40+
return this.em.createQuery("select count(x) from Account x", Long.class)
41+
.getSingleResult();
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.test.wildflytransactions;
2+
3+
import java.util.Properties;
4+
5+
import javax.sql.DataSource;
6+
7+
import org.hibernate.engine.transaction.jta.platform.internal.JBossAppServerJtaPlatform;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.ComponentScan;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
12+
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
13+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
14+
import org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor;
15+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
16+
import org.springframework.transaction.annotation.EnableTransactionManagement;
17+
import org.springframework.transaction.jta.JtaTransactionManager;
18+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
19+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
20+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
21+
22+
@Configuration
23+
@EnableWebMvc
24+
@EnableTransactionManagement
25+
@ComponentScan
26+
public class Config {
27+
28+
@Bean
29+
public DataSource dataSource() {
30+
JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
31+
return dataSourceLookup.getDataSource("java:jboss/datasources/springdemo");
32+
}
33+
34+
@Bean
35+
public JtaTransactionManager transactionManager() {
36+
return new JtaTransactionManager();
37+
}
38+
39+
@Bean
40+
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
41+
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
42+
em.setJtaDataSource(dataSource());
43+
em.setPackagesToScan(new String[] { "com.test.wildflytransactions" });
44+
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
45+
vendorAdapter.setShowSql(true);
46+
vendorAdapter.setGenerateDdl(true);
47+
em.setJpaVendorAdapter(vendorAdapter);
48+
em.setJpaProperties(additionalProperties());
49+
return em;
50+
}
51+
52+
@Bean
53+
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
54+
return new PersistenceExceptionTranslationPostProcessor();
55+
}
56+
57+
Properties additionalProperties() {
58+
Properties properties = new Properties();
59+
properties.put("hibernate.transaction.jta.platform",
60+
JBossAppServerJtaPlatform.class.getName());
61+
return properties;
62+
}
63+
64+
@Configuration
65+
protected static class JpaWebConfiguration extends WebMvcConfigurerAdapter {
66+
67+
@Override
68+
public void addInterceptors(InterceptorRegistry registry) {
69+
registry.addWebRequestInterceptor(openEntityManagerInViewInterceptor());
70+
}
71+
72+
@Bean
73+
public OpenEntityManagerInViewInterceptor openEntityManagerInViewInterceptor() {
74+
return new OpenEntityManagerInViewInterceptor();
75+
}
76+
77+
}
78+
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.test.wildflytransactions;
2+
3+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
4+
5+
public class Initiializer extends AbstractAnnotationConfigDispatcherServletInitializer {
6+
7+
@Override
8+
protected Class<?>[] getRootConfigClasses() {
9+
return new Class<?>[] { Config.class };
10+
}
11+
12+
@Override
13+
protected Class<?>[] getServletConfigClasses() {
14+
return null;
15+
}
16+
17+
@Override
18+
protected String[] getServletMappings() {
19+
return new String[] { "/" };
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.test.wildflytransactions;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.ResponseBody;
7+
8+
@Controller
9+
public class WebController {
10+
11+
@Autowired
12+
private AccountService service;
13+
14+
@RequestMapping("/")
15+
@ResponseBody
16+
public String test() {
17+
System.out.println("Count is " + this.service.getCount());
18+
this.service.createAccountAndNotify("josh");
19+
try {
20+
this.service.createAccountAndNotify("error");
21+
}
22+
catch (Exception ex) {
23+
System.out.println(ex.getMessage());
24+
}
25+
long count = this.service.getCount();
26+
System.out.println("Count is " + count);
27+
return "Count is " + count;
28+
}
29+
30+
}

0 commit comments

Comments
 (0)