我正在尝试将数据从一个数据库复制到另一个数据库。一切工作正常,直到修改了源数据库中的一行之一(下面的堆栈跟踪)。按预期将新行添加到目标数据库。
对于每个数据库连接(mysql和hsqldb),我都有一个由以下bean组成的上下文文件:
<bean id="mysqlDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${mysql.driver}" />
<property name="url" value="${mysql.url}" />
<property name="username" value="${mysql.username}" />
<property name="password" value="${mysql.password}" />
</bean>
<bean id="mysqlTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="mysqlEntityManagerFactory" />
<property name="dataSource" ref="mysqlDataSource" />
</bean>
<bean id="mysqlEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="mysqlDataSource" />
<property name="jpaVendorAdapter" ref="mysqlVendorAdaptor" />
<property name="packagesToScan" value="my.packages.to.scan"/>
</bean>
<bean id="mysqlEntityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="mysqlEntityManagerFactory"/>
</bean>
<bean id="mysqlVendorAdaptor" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="${mysql.dialect}" />
</bean>
在我为每个数据库连接中@Service
创建一个JpaRepository
:
@Autowired
private EntityManager hsqldbEntityManager;
@Autowired
private EntityManager mysqlEntityManager;
private MyRepository hsqldbDao;
private MyRepository mysqlDao;
public void init() {
hsqldbDao = new JpaRepositoryFactory(hsqldbEntityManager).getRepository(MyRepository.class);
mysqlDao = new JpaRepositoryFactory(mysqlEntityManager).getRepository(MyRepository.class);
}
MyRepository
很简单:
public interface MyRepository extends JpaRepository<MyClass, byte[]>{ }
我正在使用Spring Integration将2种服务方法连接在一起。服务方法如下:
@Transactional("mysqlTransactionManager")
public List<MyClass> findAll() {
List<MyClass> list= mysqlDao.findAll();
return list;
}
@Transactional("hsqldbTransactionManager")
public void persist(List<MyClass> list) {
hsqldbDao.save(list);
hsqldbDao.flush();
}
我尝试过从EntityManager
s 分离列表,并在保存之前深度克隆该列表,但存在相同的错误。因此,我认为这与我配置事务/实体经理的方式有关。
任何帮助表示赞赏。
堆栈跟踪:
[task-scheduler-1] ERROR org.springframework.integration.handler.LoggingHandler - org.springframework.integration.MessageHandlingException: javax.persistence.OptimisticLockException
at org.springframework.integration.handler.MethodInvokingMessageProcessor.processMessage(MethodInvokingMessageProcessor.java:76)
at org.springframework.integration.handler.MethodInvokingMessageHandler.handleMessageInternal(MethodInvokingMessageHandler.java:59)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:73)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:115)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:102)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:157)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:128)
at org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:288)
at org.springframework.integration.core.MessagingTemplate.send(MessagingTemplate.java:149)
at org.springframework.integration.endpoint.SourcePollingChannelAdapter.handleMessage(SourcePollingChannelAdapter.java:97)
at org.springframework.integration.endpoint.AbstractTransactionSynchronizingPollingEndpoint.doPoll(AbstractTransactionSynchronizingPollingEndpoint.java:82)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:146)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:144)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller$1.run(AbstractPollingEndpoint.java:236)
at org.springframework.integration.util.ErrorHandlingTaskExecutor$1.run(ErrorHandlingTaskExecutor.java:52)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:48)
at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:49)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.run(AbstractPollingEndpoint.java:231)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:53)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: javax.persistence.OptimisticLockException
at org.hibernate.ejb.AbstractEntityManagerImpl.wrapStaleStateException(AbstractEntityManagerImpl.java:1241)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1167)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1148)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1154)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:695)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
at $Proxy21.merge(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:353)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:384)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:333)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:318)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeRepositoryPostProcessor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy22.save(Unknown Source)
at my.package.MyService.persist(xxxxx)
at my.package.MyService$$FastClassByCGLIB$$c484c23b.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
at my.package.MyService$$EnhancerByCGLIB$$ed6874cf.persist(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.expression.spel.support.ReflectiveMethodExecutor.execute(ReflectiveMethodExecutor.java:69)
at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:84)
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:57)
at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:102)
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:102)
at org.springframework.integration.util.AbstractExpressionEvaluator.evaluateExpression(AbstractExpressionEvaluator.java:126)
at org.springframework.integration.util.MessagingMethodInvokerHelper.processInternal(MessagingMethodInvokerHelper.java:227)
at org.springframework.integration.util.MessagingMethodInvokerHelper.process(MessagingMethodInvokerHelper.java:127)
at org.springframework.integration.handler.MethodInvokingMessageProcessor.processMessage(MethodInvokingMessageProcessor.java:73)
... 28 more
Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [model.MyClass#[B@d76d1e]
at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:485)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:255)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:84)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:867)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:851)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:855)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:686)
... 71 more
您已使用带@Version
注释的字段在实体上配置了乐观锁定。乐观锁定的原理是,仅当行中存储的版本与合并实体中存储的版本具有相同的值时,才允许更新该行。否则,将抛出您得到的异常。
因此,如果要绕过此检查,则必须从实体获取版本进行更新,将其复制到正在合并的分离实体中(就像您从目标数据库中加载了该实体一样),然后合并该实体。
问题内容: 如何从一个数据库复制到另一个数据库。 数据库名称visco 我想将所有表从visco数据库复制到新数据库名称作为neptune 我创建了一个没有任何表的数据库,然后尝试从database1.bak文件还原数据库,然后显示错误为 需要查询帮助 问题答案: 使用SQL Server Management Studio: 选项1 ->右键单击要复制的数据库 ->选择“任务”>“生成脚本” -
问题内容: 我需要将数据从一个表复制到另一个表。这两个表几乎具有相同的结构,但是位于不同的数据库中。 我试过了 我尝试此操作,但跨数据库出现错误…未实现 问题答案: 这是一个非常简单的任务。只需为此目的使用dblink: 如果您需要定期从外部数据库中获取数据,明智的做法是定义服务器和用户映射。然后,您可以使用较短的语句:
问题内容: 我需要将表从一个数据库复制到另一个数据库。这将是一个cronjob。哪一种是最好的方法?PHP脚本或Shell脚本。PHP的问题是,两个数据库都有不同的用户名和密码,所以我不能这样做。 我应该只连接第一个DB以获得所有记录,然后使用WHILE循环将所有记录插入新数据库,还是有更好的方法? 我更喜欢用shell脚本代替PHP脚本来执行此操作。 谢谢 问题答案: 我把它丢了。比任何基于PH
问题内容: 我有两个数据库,一个叫做,一个叫做。由于我想练习插入,更新内容,因此我想将某些表从复制到。 我要复制的表称为: 我试图做的是(使用SSMS)右键单击表格,但其中没有“复制”! 问题答案: 假设您有两个数据库,例如A和B: 如果目标表不存在,将创建以下脚本(我不建议这样): INTO COPY_TABLE_HERE FROM A.dbo.table_from_A table_A 如果目标
有没有一个简单的方法可以做到这一点?
问题内容: 我需要将数据从一个数据库迁移到另一个数据库,两者都在同一本地系统上。 表和列的名称不同,我不能从旧数据库迁移所有列,因此 对我不起作用。 但我只有一个 我的查询中有什么错误,我该如何解决? 提前致谢 问题答案: 您的查询应如下所示: 更新 由于这个答案的关注程度超出了我的预期,因此我应该扩展这个答案。首先,从答案本身看可能并不明显,但各列不必具有相同的名称。因此,以下操作也将起作用(假