mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Compare commits
35
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7133c81fc5 | ||
|
|
c6b4de39e8 | ||
|
|
94f367af70 | ||
|
|
dc6c393300 | ||
|
|
239f3a0a54 | ||
|
|
d3e78ab54e | ||
|
|
c6483535bd | ||
|
|
95bb2a5d56 | ||
|
|
23649ed840 | ||
|
|
3ee4034a76 | ||
|
|
21d377546e | ||
|
|
8c9dccc6a3 | ||
|
|
6fdf681993 | ||
|
|
7e8f413578 | ||
|
|
6705e5a962 | ||
|
|
d9126a936d | ||
|
|
4ee024b1ce | ||
|
|
19491d5ae6 | ||
|
|
db816594c5 | ||
|
|
fa73b7791f | ||
|
|
c61da5b0e5 | ||
|
|
49d724ad65 | ||
|
|
3ee2410a79 | ||
|
|
c2ddc70565 | ||
|
|
2f66e07c25 | ||
|
|
0807569ee2 | ||
|
|
206dec1bf8 | ||
|
|
0e852f5bb5 | ||
|
|
a272fb4e02 | ||
|
|
cbdc6e09aa | ||
|
|
211087f636 | ||
|
|
dca824b431 | ||
|
|
6bcc8eb0f2 | ||
|
|
844df4d5c9 | ||
|
|
6360d5fe1c |
@@ -1,4 +1,29 @@
|
||||
avaje-ebeanorm-spring
|
||||
ebean-spring-txn
|
||||
=====================
|
||||
|
||||
Spring integration with EbeanORM
|
||||
Integration with Spring managed transactions.
|
||||
|
||||
This project provides an Ebean `ExternalTransactionManager` specifically
|
||||
to integrate with Springs JDBC Transaction manager.
|
||||
|
||||
## To use
|
||||
|
||||
```java
|
||||
ServerConfig serverConfig = new ServerConfig();
|
||||
|
||||
// set SpringJdbcTransactionManager ... as the external transaction manager
|
||||
serverConfig.setExternalTransactionManager(new SpringJdbcTransactionManager());
|
||||
|
||||
...
|
||||
EbeanServer server = EbeanServerFactory.create(serverConfig);
|
||||
|
||||
```
|
||||
|
||||
## Notes
|
||||
|
||||
You can use Ebean in Spring/Spring Boot *without* this and that case Ebean
|
||||
manages the Transactions itself. With Ebean managing the transactions there
|
||||
are some benefits with more control over JDBC batch, getGeneratedKeys
|
||||
and a simpler abstraction (as Spring transactions is designed to manage
|
||||
multiple resources such as JDBC Transactions and JPA EntityManager and Ebean
|
||||
only needs to manage JDBC Transactions).
|
||||
@@ -4,13 +4,13 @@
|
||||
|
||||
<parent>
|
||||
<groupId>org.avaje</groupId>
|
||||
<artifactId>java8-parent</artifactId>
|
||||
<version>1.3</version>
|
||||
<artifactId>java8-oss</artifactId>
|
||||
<version>2.2</version>
|
||||
</parent>
|
||||
|
||||
<groupId>io.ebean</groupId>
|
||||
<artifactId>ebean-spring-txn</artifactId>
|
||||
<version>10.1.1</version>
|
||||
<version>12.4.2</version>
|
||||
|
||||
<name>ebean-spring-txn</name>
|
||||
<packaging>jar</packaging>
|
||||
@@ -24,22 +24,22 @@
|
||||
<url>http://ebean-orm.github.io/</url>
|
||||
|
||||
<scm>
|
||||
<developerConnection>scm:git:git@github.com:ebean-orm/ebean-spring.git</developerConnection>
|
||||
<tag>ebean-spring-txn-10.1.1</tag>
|
||||
<developerConnection>scm:git:git@github.com:ebean-orm/ebean-spring-txn.git</developerConnection>
|
||||
<tag>ebean-spring-txn-12.4.2</tag>
|
||||
</scm>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.21</version>
|
||||
<version>1.7.25</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.ebean</groupId>
|
||||
<artifactId>ebean</artifactId>
|
||||
<version>[10,)</version>
|
||||
<version>12.4.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
@@ -79,10 +79,30 @@
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.avaje.composite</groupId>
|
||||
<artifactId>avaje-composite-testing-ebean</artifactId>
|
||||
<version>4.1</version>
|
||||
<type>pom</type>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.4.196</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.2.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
@@ -100,11 +120,11 @@
|
||||
<plugin>
|
||||
<groupId>io.repaint.maven</groupId>
|
||||
<artifactId>tiles-maven-plugin</artifactId>
|
||||
<version>2.8</version>
|
||||
<version>2.17</version>
|
||||
<extensions>true</extensions>
|
||||
<configuration>
|
||||
<tiles>
|
||||
<tile>io.ebean.tile:enhancement:2.1</tile>
|
||||
<tile>io.ebean.tile:enhancement:12.4.1</tile>
|
||||
</tiles>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
@@ -1,27 +1,8 @@
|
||||
/**
|
||||
* Copyright (C) 2009 the original author or authors
|
||||
* <p>
|
||||
* This file is part of Ebean.
|
||||
* <p>
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
* <p>
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
* <p>
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package io.ebean.spring.txn;
|
||||
|
||||
import io.ebean.TxScope;
|
||||
import io.ebean.config.ExternalTransactionManager;
|
||||
import io.ebeaninternal.api.SpiTransaction;
|
||||
import io.ebeaninternal.server.transaction.DefaultTransactionThreadLocal;
|
||||
import io.ebeaninternal.server.transaction.TransactionManager;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -57,11 +38,6 @@ public class SpringJdbcTransactionManager implements ExternalTransactionManager
|
||||
*/
|
||||
private TransactionManager transactionManager;
|
||||
|
||||
/**
|
||||
* The EbeanServer name.
|
||||
*/
|
||||
private String serverName;
|
||||
|
||||
/**
|
||||
* Instantiates a new spring aware transaction scope manager.
|
||||
*/
|
||||
@@ -73,13 +49,10 @@ public class SpringJdbcTransactionManager implements ExternalTransactionManager
|
||||
*/
|
||||
@Override
|
||||
public void setTransactionManager(Object txnMgr) {
|
||||
|
||||
// RB: At this stage not exposing TransactionManager to
|
||||
// the public API and hence the Object type and casting here
|
||||
|
||||
this.transactionManager = (TransactionManager) txnMgr;
|
||||
this.dataSource = transactionManager.getDataSource();
|
||||
this.serverName = transactionManager.getServerName();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,19 +63,16 @@ public class SpringJdbcTransactionManager implements ExternalTransactionManager
|
||||
*/
|
||||
@Override
|
||||
public Object getCurrentTransaction() {
|
||||
|
||||
// Get the current Spring ConnectionHolder associated to the current spring managed transaction
|
||||
ConnectionHolder holder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);
|
||||
|
||||
if (holder == null || !holder.isSynchronizedWithTransaction()) {
|
||||
// no current Spring transaction
|
||||
SpiTransaction currentEbeanTransaction = DefaultTransactionThreadLocal.get(serverName);
|
||||
if (currentEbeanTransaction != null) { // this is unexpected
|
||||
log.warn("No current Spring transaction BUT using current Ebean one {}", currentEbeanTransaction.getId());
|
||||
SpiTransaction currentEbeanTransaction = transactionManager.getInScope();
|
||||
if (currentEbeanTransaction == null || !currentEbeanTransaction.isActive()) {
|
||||
return null;
|
||||
} else {
|
||||
log.trace("No current Spring transaction");
|
||||
return currentEbeanTransaction;
|
||||
}
|
||||
return currentEbeanTransaction;
|
||||
}
|
||||
|
||||
SpringTxnListener springTxnLister = getSpringTxnListener();
|
||||
@@ -117,10 +87,7 @@ public class SpringJdbcTransactionManager implements ExternalTransactionManager
|
||||
// Create and register a Spring TransactionSynchronization for this transaction
|
||||
springTxnLister = createSpringTxnListener(newTrans);
|
||||
TransactionSynchronizationManager.registerSynchronization(springTxnLister);
|
||||
|
||||
// also put in Ebean ThreadLocal
|
||||
DefaultTransactionThreadLocal.set(serverName, newTrans);
|
||||
return newTrans;
|
||||
return transactionManager.externalBeginTransaction(newTrans, TxScope.required());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,7 +98,6 @@ public class SpringJdbcTransactionManager implements ExternalTransactionManager
|
||||
* </p>
|
||||
*/
|
||||
private SpringTxnListener getSpringTxnListener() {
|
||||
|
||||
if (TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager.getSynchronizations();
|
||||
if (synchronizations != null) {
|
||||
@@ -143,7 +109,6 @@ public class SpringJdbcTransactionManager implements ExternalTransactionManager
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -174,18 +139,15 @@ public class SpringJdbcTransactionManager implements ExternalTransactionManager
|
||||
|
||||
private final SpringJdbcTransaction transaction;
|
||||
|
||||
private final String serverName;
|
||||
|
||||
private SpringTxnListener(TransactionManager transactionManager, SpringJdbcTransaction t) {
|
||||
this.transactionManager = transactionManager;
|
||||
this.transaction = t;
|
||||
this.serverName = transactionManager.getServerName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the associated Ebean wrapped transaction.
|
||||
*/
|
||||
public SpringJdbcTransaction getTransaction() {
|
||||
SpringJdbcTransaction getTransaction() {
|
||||
return transaction;
|
||||
}
|
||||
|
||||
@@ -196,13 +158,13 @@ public class SpringJdbcTransactionManager implements ExternalTransactionManager
|
||||
|
||||
@Override
|
||||
public void beforeCommit(boolean readOnly) {
|
||||
// Future note: for JPA2 locking we will
|
||||
// have beforeCommit events to fire
|
||||
if (!readOnly) {
|
||||
transaction.flushBatch();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(int status) {
|
||||
|
||||
switch (status) {
|
||||
case STATUS_COMMITTED:
|
||||
log.debug("Spring Txn [{}] committed", transaction.getId());
|
||||
@@ -220,7 +182,7 @@ public class SpringJdbcTransactionManager implements ExternalTransactionManager
|
||||
}
|
||||
|
||||
// Remove this transaction object as it is completed
|
||||
DefaultTransactionThreadLocal.replace(serverName, null);
|
||||
transactionManager.externalRemoveTransaction();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,41 +1,15 @@
|
||||
/**
|
||||
* Copyright (C) 2009 the original author or authors
|
||||
* <p>
|
||||
* This file is part of Ebean.
|
||||
* <p>
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
* <p>
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
* <p>
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.example;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
/**
|
||||
* The Class Account.
|
||||
* @since 18.05.2009
|
||||
* @author E Mc Greal
|
||||
*/
|
||||
@Entity
|
||||
public class Account {
|
||||
|
||||
/** The id. */
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
/** The user. */
|
||||
@OneToOne
|
||||
private User user;
|
||||
|
||||
|
||||
@@ -1,24 +1,6 @@
|
||||
/**
|
||||
* Copyright (C) 2009 the original author or authors
|
||||
* <p>
|
||||
* This file is part of Ebean.
|
||||
* <p>
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
* <p>
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
* <p>
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.example;
|
||||
|
||||
import io.ebean.annotation.PersistBatch;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
@@ -50,6 +32,7 @@ public class EbeanServerFactoryBean implements InitializingBean, FactoryBean<Ebe
|
||||
throw new Exception("No ServerConig set. You must define a ServerConfig bean");
|
||||
}
|
||||
|
||||
serverConfig.setPersistBatch(PersistBatch.ALL);
|
||||
// Create the new EbeanServer using the configuration
|
||||
this.ebeanServer = EbeanServerFactory.create(serverConfig);
|
||||
}
|
||||
|
||||
@@ -1,30 +1,17 @@
|
||||
/**
|
||||
* Copyright (C) 2009 the original author or authors
|
||||
* <p>
|
||||
* This file is part of Ebean.
|
||||
* <p>
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
* <p>
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
* <p>
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.example;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.Transaction;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@@ -38,7 +25,7 @@ import static org.junit.Assert.assertTrue;
|
||||
public class EbeanSpringModuleTest {
|
||||
|
||||
/** The Constant logger. */
|
||||
private final static Logger logger = Logger.getLogger(EbeanSpringModuleTest.class.getName());
|
||||
private final static Logger logger = LoggerFactory.getLogger(EbeanSpringModuleTest.class);
|
||||
|
||||
/** The user service. */
|
||||
@Autowired
|
||||
@@ -51,6 +38,27 @@ public class EbeanSpringModuleTest {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test app.
|
||||
*/
|
||||
@Test
|
||||
public void testInactiveTransaction() {
|
||||
|
||||
Transaction transaction = Ebean.beginTransaction();
|
||||
long id;
|
||||
try {
|
||||
User user = new User();
|
||||
user.setName("save with txn 1");
|
||||
Ebean.save(user);
|
||||
transaction.commit();
|
||||
id = user.getOid();
|
||||
} finally {
|
||||
// don't end the transaction ...
|
||||
//transaction.end();
|
||||
}
|
||||
|
||||
Ebean.delete(User.class, id);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBatchInsert() {
|
||||
@@ -68,6 +76,10 @@ public class EbeanSpringModuleTest {
|
||||
user.setName("ebean");
|
||||
userService.save(user);
|
||||
logger.info("Saved new User");
|
||||
assertThat(user.getOid()).isGreaterThan(0);
|
||||
|
||||
final User found = DB.find(User.class, user.getOid());
|
||||
assertThat(found).isNotNull();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,6 +95,20 @@ public class EbeanSpringModuleTest {
|
||||
logger.info("Found User with OID = 1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonTransactional() {
|
||||
userService.nonTransactional();
|
||||
logger.info("nonTransactional done");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindNonTransactional() {
|
||||
logger.info("Finding User with OID = 1 ...");
|
||||
User user = userService.find(1);
|
||||
User user2 = userService.findNoCurrentTransaction(2);
|
||||
logger.info("nonTransactional find user1:{} user2:{}", user, user2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the user service.
|
||||
*/
|
||||
|
||||
@@ -1,23 +1,3 @@
|
||||
/**
|
||||
* Copyright (C) 2009 the original author or authors
|
||||
* <p>
|
||||
* This file is part of Ebean.
|
||||
* <p>
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
* <p>
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
* <p>
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package org.example;
|
||||
|
||||
import java.util.Set;
|
||||
@@ -25,11 +5,6 @@ import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToMany;
|
||||
|
||||
/**
|
||||
* The Class Role.
|
||||
* @since 18.05.2009
|
||||
* @author E Mc Greal
|
||||
*/
|
||||
@Entity
|
||||
public class Role {
|
||||
|
||||
|
||||
@@ -1,22 +1,3 @@
|
||||
/**
|
||||
* Copyright (C) 2009 the original author or authors
|
||||
*
|
||||
* This file is part of Ebean.
|
||||
*
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.example;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
@@ -25,13 +6,9 @@ import javax.persistence.ManyToMany;
|
||||
import javax.persistence.OneToOne;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* The Class User.
|
||||
* @since 18.05.2009
|
||||
* @author E Mc Greal
|
||||
*/
|
||||
@Entity
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
long oid;
|
||||
|
||||
@@ -40,9 +17,13 @@ public class User {
|
||||
@ManyToMany
|
||||
private Set<Role> roles;
|
||||
|
||||
@OneToOne(mappedBy="user")
|
||||
@OneToOne(mappedBy = "user")
|
||||
private Account account;
|
||||
|
||||
public String toString() {
|
||||
return "{user:" + oid + " name:" + name + "}";
|
||||
}
|
||||
|
||||
public long getOid() {
|
||||
return oid;
|
||||
}
|
||||
|
||||
@@ -1,35 +1,17 @@
|
||||
/**
|
||||
* Copyright (C) 2009 the original author or authors
|
||||
*
|
||||
* This file is part of Ebean.
|
||||
*
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package org.example;
|
||||
|
||||
/**
|
||||
* The Interface UserService.
|
||||
* @since 18.05.2009
|
||||
* @author E Mc Greal
|
||||
*/
|
||||
public interface UserService {
|
||||
|
||||
void save(User user);
|
||||
void save(User user);
|
||||
|
||||
User find(long id);
|
||||
|
||||
void batchInsert();
|
||||
User findNoCurrentTransaction(long id);
|
||||
|
||||
void nonTransactional();
|
||||
|
||||
void batchInsert();
|
||||
}
|
||||
|
||||
@@ -1,27 +1,6 @@
|
||||
/**
|
||||
* Copyright (C) 2009 the original author or authors
|
||||
*
|
||||
* This file is part of Ebean.
|
||||
*
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package org.example;
|
||||
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.PersistBatch;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -43,10 +22,7 @@ public class UserServiceImpl implements UserService {
|
||||
@Autowired
|
||||
private EbeanServer ebeanServer;
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spring.modules.ebean.UserService#save(org.spring.modules.ebean.User)
|
||||
*/
|
||||
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor=Throwable.class)
|
||||
@Transactional(propagation = Propagation.REQUIRED, rollbackFor=Throwable.class)
|
||||
public void save(User user) {
|
||||
ebeanServer.save(user);
|
||||
}
|
||||
@@ -56,12 +32,19 @@ public class UserServiceImpl implements UserService {
|
||||
return ebeanServer.find(User.class, id);
|
||||
}
|
||||
|
||||
public void nonTransactional() {
|
||||
ebeanServer.currentTransaction();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
|
||||
public void batchInsert(){
|
||||
public User findNoCurrentTransaction(long id) {
|
||||
return ebeanServer.find(User.class, id);
|
||||
}
|
||||
|
||||
List<User> users = new ArrayList<User>();
|
||||
for(int i=0 ;i<5;i++){
|
||||
@Transactional(propagation = Propagation.REQUIRED)
|
||||
public void batchInsert() {
|
||||
|
||||
List<User> users = new ArrayList<>();
|
||||
for(int i=0 ;i<25;i++){
|
||||
User user = new User();
|
||||
user.setName("user"+i);
|
||||
users.add(user);
|
||||
@@ -69,11 +52,9 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
System.out.println("---------before batch-------");
|
||||
|
||||
Transaction tx = ebeanServer.beginTransaction();
|
||||
tx.setBatch(PersistBatch.NONE);
|
||||
tx.setBatchOnCascade(PersistBatch.ALL);
|
||||
Transaction tx = ebeanServer.currentTransaction();
|
||||
tx.setBatchSize(20);
|
||||
ebeanServer.saveAll(users);//
|
||||
ebeanServer.saveAll(users);
|
||||
|
||||
System.out.println("---------after batch-------");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<configuration scan="true" scanPeriod="10 seconds">
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
|
||||
<level>TRACE</level>
|
||||
</filter>
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
|
||||
<logger name="io.ebean" level="INFO"/>
|
||||
|
||||
<!-- sql and transaction logging -->
|
||||
<logger name="io.ebean.SQL" level="TRACE"/>
|
||||
<logger name="io.ebean.TXN" level="TRACE"/>
|
||||
<logger name="io.ebean.SUM" level="TRACE"/>
|
||||
<logger name="io.ebean.DDL" level="TRACE"/>
|
||||
|
||||
<!-- l2 cache logging -->
|
||||
<logger name="io.ebean.cache.QUERY" level="TRACE"/>
|
||||
<logger name="io.ebean.cache.BEAN" level="TRACE"/>
|
||||
<logger name="io.ebean.cache.COLL" level="TRACE"/>
|
||||
<logger name="io.ebean.cache.NATKEY" level="TRACE"/>
|
||||
|
||||
<!-- elasticSearch query logging -->
|
||||
<!--<logger name="io.ebean.ELA" level="TRACE"/>-->
|
||||
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user