gpt4 book ai didi

java - HttpSessionListener 中的事务提交不会持久保存到数据库

转载 作者:行者123 更新时间:2023-11-28 22:31:29 25 4
gpt4 key购买 nike

我最近开始使用 JBossTM 以便在多个数据库连接上使用 JTA,这非常成功。如果我尝试从任何 Controller 执行事务,它可以正常工作。

但是,我最近开始尝试从 HttpSessionListener 的实例中持久化实体,以便我可以跟踪用户 session ,但我无法使持久化工作。我确定这里缺少一些非常简单的东西,但欢迎提供任何提示。

问题是,虽然一切似乎都运行正常(日志明智),但实际上没有任何内容提交到数据库。我以前在尝试持久化到没有事务管理器的数据库时看到过这个错误(在我切换到 JBossTM 之前),但似乎无法破解这个错误。

这是我的 application-context.xml 的相关部分:

<tx:annotation-driven transaction-manager="transactionManager" />

<bean class="com.arjuna.ats.jta.TransactionManager" factory-method="transactionManager" id="arjunaTransactionManager"/>
<bean class="com.arjuna.ats.jta.UserTransaction" factory-method="userTransaction" id="arjunaUserTransaction"/>

<bean class="org.springframework.transaction.jta.JtaTransactionManager" id="transactionManager">
<property name="transactionManager">
<ref bean="arjunaTransactionManager"/>
</property>
<property name="userTransaction">
<ref bean="arjunaUserTransaction"/>
</property>
</bean>

<jee:jndi-lookup id="CoreDS" jndi-name="java:/comp/env/jdbc/CoreDS" resource-ref="true" />

<bean id="coreEMW" class="org.springframework.orm.jpa.support.SharedEntityManagerBean"
p:entityManagerFactory-ref="coreEMF"/>

<bean id="coreEMF" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="CoreDS"/>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>

<bean id="sessionDAO" class="core.dao.SessionDAOImpl"
p:entityManager-ref="coreEMW" />

<bean name="openEntityManagerInViewInterceptor"
class="org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor"
p:entityManagerFactory-ref="coreEMF" />

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

<bean id='jdbcTemplate' class='org.springframework.jdbc.core.JdbcTemplate'
p:dataSource-ref='CoreDS' />

网络.xml

<listener>
<listener-class>core.util.SessionListener</listener-class>
</listener>

session 监听器.java

public class SessionListener implements HttpSessionListener {
private static Log log = LogFactory.getLog(SessionListener.class);

@Override
@Transactional
public void sessionCreated(HttpSessionEvent event) {
getDAO(event).save(new Session(event.getSession().getId(),Calendar.getInstance()));
}

@Override
public void sessionDestroyed(HttpSessionEvent event) {
....
}

private SessionDAO getDAO(HttpSessionEvent event){
HttpSession session = event.getSession();

ApplicationContext ctx =
WebApplicationContextUtils.
getWebApplicationContext(session.getServletContext());

SessionDAO dao =
(SessionDAO) ctx.getBean("sessionDAO");

return dao;
}
}

持久性.xml

<persistence version="1.0" xmlns="...">
<persistence-unit name="BPSCorePU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>java:comp/env/jdbc/BPSCoreDS</non-jta-data-source>
...
<class>core.model.Session</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
</persistence>

日志(尽管您可以看到提交,但 Session 对象未持久化)

2013-04-18 15:47:41,763 [core.util.SessionListener] TRACE - sessionCreated(): C5A37E5CC12FF19C277769DA8B97BF0A
2013-04-18 15:47:41,798 [com.arjuna.ats.jta] TRACE - BaseTransaction.getStatus
2013-04-18 15:47:41,854 [com.arjuna.ats.arjuna] TRACE - Loading class com.arjuna.ats.internal.arjuna.coordinator.CheckedActionFactoryImple
2013-04-18 15:47:41,873 [com.arjuna.ats.jta] TRACE - BaseTransaction.begin
2013-04-18 15:47:41,882 [com.arjuna.ats.arjuna] TRACE - Loading class com.arjuna.ats.arjuna.recovery.ActionStatusService
2013-04-18 15:47:41,889 [com.arjuna.ats.arjuna] TRACE - Loading class com.arjuna.ats.internal.arjuna.objectstore.ShadowNoFileLockStore
2013-04-18 15:47:41,891 [com.arjuna.ats.arjuna] TRACE - FileSystemStore.createHierarchy(/tmp/jbosstm/tx/a/ShadowNoFileLockStore/defaultStore/)
2013-04-18 15:47:41,900 [com.arjuna.ats.arjuna] INFO - ARJUNA012163: Starting service com.arjuna.ats.arjuna.recovery.ActionStatusService on port 32882
2013-04-18 15:47:41,910 [com.arjuna.ats.arjuna] TRACE - Loading class com.arjuna.ats.internal.arjuna.utils.SocketProcessId
2013-04-18 15:47:41,912 [com.arjuna.ats.arjuna] INFO - ARJUNA012337: TransactionStatusManagerItem host: 127.0.0.1 port: 32882
2013-04-18 15:47:41,913 [com.arjuna.ats.arjuna] TRACE - OutputObjectState::OutputObjectState()
2013-04-18 15:47:41,914 [com.arjuna.ats.arjuna] TRACE - Loading class com.arjuna.ats.internal.arjuna.objectstore.ShadowNoFileLockStore
2013-04-18 15:47:41,914 [com.arjuna.ats.arjuna] TRACE - FileSystemStore.createHierarchy(/tmp/jbosstm/tx/a/ShadowNoFileLockStore/defaultStore/)
2013-04-18 15:47:41,915 [com.arjuna.ats.arjuna] TRACE - FileSystemStore.write_committed(0:ffffc0a80036:8073:5170078d:0, /Recovery/TransactionStatusManager)
2013-04-18 15:47:41,916 [com.arjuna.ats.arjuna] TRACE - ShadowingStore.write_state(0:ffffc0a80036:8073:5170078d:0, /Recovery/TransactionStatusManager, StateType.OS_ORIGINAL)
2013-04-18 15:47:41,916 [com.arjuna.ats.arjuna] TRACE - ShadowingStore.genPathName(0:ffffc0a80036:8073:5170078d:0, /Recovery/TransactionStatusManager, StateType.OS_ORIGINAL)
2013-04-18 15:47:41,916 [com.arjuna.ats.arjuna] TRACE - FileSystemStore.genPathName(0:ffffc0a80036:8073:5170078d:0, /Recovery/TransactionStatusManager, 11)
2013-04-18 15:47:41,918 [com.arjuna.ats.arjuna] TRACE - FileSystemStore.openAndLock(/tmp/jbosstm/tx/a/ShadowNoFileLockStore/defaultStore/Recovery/TransactionStatusManager/0_ffffc0a80036_8073_5170078d_0, FileLock.F_WRLCK, true)
2013-04-18 15:47:41,918 [com.arjuna.ats.arjuna] TRACE - FileSystemStore.createHierarchy(/tmp/jbosstm/tx/a/ShadowNoFileLockStore/defaultStore/Recovery/TransactionStatusManager/0_ffffc0a80036_8073_5170078d_0)
2013-04-18 15:47:41,927 [com.arjuna.ats.arjuna] TRACE - FileSystemStore.closeAndUnlock(/tmp/jbosstm/tx/a/ShadowNoFileLockStore/defaultStore/Recovery/TransactionStatusManager/0_ffffc0a80036_8073_5170078d_0, null, java.io.FileOutputStream@283a0f10)
2013-04-18 15:47:41,927 [com.arjuna.ats.arjuna] INFO - ARJUNA012170: TransactionStatusManager started on port 32882 and host 127.0.0.1 with service com.arjuna.ats.arjuna.recovery.ActionStatusService
2013-04-18 15:47:41,928 [com.arjuna.ats.arjuna] TRACE - StateManager::StateManager( 2, 0 )
2013-04-18 15:47:41,928 [com.arjuna.ats.arjuna] TRACE - BasicAction::BasicAction()
2013-04-18 15:47:41,929 [com.arjuna.ats.arjuna] TRACE - BasicAction::Begin() for action-id 0:ffffc0a80036:8073:5170078d:2
2013-04-18 15:47:41,929 [com.arjuna.ats.arjuna] TRACE - BasicAction::actionInitialise() for action-id 0:ffffc0a80036:8073:5170078d:2
2013-04-18 15:47:41,930 [com.arjuna.ats.arjuna] TRACE - ActionHierarchy::ActionHierarchy(1)
2013-04-18 15:47:41,931 [com.arjuna.ats.arjuna] TRACE - ActionHierarchy::add(0:ffffc0a80036:8073:5170078d:2, 1)
2013-04-18 15:47:41,934 [com.arjuna.ats.arjuna] TRACE - BasicAction::addChildThread () action 0:ffffc0a80036:8073:5170078d:2 adding Thread["ajp-bio-8010"-exec-1,5,main]
2013-04-18 15:47:41,935 [com.arjuna.ats.arjuna] TRACE - BasicAction::addChildThread () action 0:ffffc0a80036:8073:5170078d:2 adding Thread["ajp-bio-8010"-exec-1,5,main] result = true
2013-04-18 15:47:41,938 [com.arjuna.ats.arjuna] TRACE - TransactionReaper::instantiate()
2013-04-18 15:47:41,939 [com.arjuna.ats.arjuna] TRACE - TransactionReaper::TransactionReaper ( 9223372036854775807 )
2013-04-18 15:47:41,941 [com.arjuna.ats.arjuna] TRACE - ReaperThread.run ()
2013-04-18 15:47:41,942 [com.arjuna.ats.arjuna] TRACE - Thread Thread[Transaction Reaper,5,main] sleeping for 9223370670558313865
2013-04-18 15:47:41,942 [com.arjuna.ats.arjuna] TRACE - TransactionReaper::insert ( BasicAction: 0:ffffc0a80036:8073:5170078d:2 status: ActionStatus.RUNNING, 60 )
2013-04-18 15:47:41,944 [com.arjuna.ats.arjuna] TRACE - ReaperElement::ReaperElement ( BasicAction: 0:ffffc0a80036:8073:5170078d:2 status: ActionStatus.RUNNING, 60 )
2013-04-18 15:47:41,944 [com.arjuna.ats.arjuna] TRACE - ReaperThread.run ()
2013-04-18 15:47:41,944 [com.arjuna.ats.arjuna] TRACE - TransactionReaper::check ()
2013-04-18 15:47:41,944 [com.arjuna.ats.arjuna] TRACE - TransactionReaper::check - comparing 1366296521944
2013-04-18 15:47:41,945 [com.arjuna.ats.arjuna] TRACE - Thread Thread[Transaction Reaper,5,main] sleeping for 59999
2013-04-18 15:47:41,946 [com.arjuna.ats.arjuna] TRACE - ReaperWorkerThread.run ()
2013-04-18 15:47:41,946 [com.arjuna.ats.arjuna] TRACE - Thread Thread[Transaction Reaper Worker 0,5,main] waiting for cancelled TXs
2013-04-18 15:47:42,091 [dao.BaseDAOImpl] TRACE - Persisting
2013-04-18 15:47:42,091 [dao.BaseDAOImpl] DEBUG - adtest Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1dcd8d1f]
2013-04-18 15:47:42,091 [dao.BaseDAOImpl] DEBUG - adtest core.model.Session@6229abca
2013-04-18 15:47:42,120 [com.arjuna.ats.jta] TRACE - BaseTransaction.getStatus
2013-04-18 15:47:42,120 [com.arjuna.ats.jta] TRACE - TransactionImple.getStatus
2013-04-18 15:47:42,121 [com.arjuna.ats.jta] TRACE - BaseTransaction.getStatus
2013-04-18 15:47:42,121 [com.arjuna.ats.jta] TRACE - TransactionImple.getStatus
2013-04-18 15:47:42,121 [com.arjuna.ats.jta] TRACE - BaseTransaction.commit
2013-04-18 15:47:42,121 [com.arjuna.ats.jta] TRACE - TransactionImple.commitAndDisassociate
2013-04-18 15:47:42,121 [com.arjuna.ats.arjuna] TRACE - BasicAction::End() for action-id 0:ffffc0a80036:8073:5170078d:2
2013-04-18 15:47:42,122 [com.arjuna.ats.arjuna] TRACE - BasicAction::removeChildThread () action 0:ffffc0a80036:8073:5170078d:2 removing TSThread:1
2013-04-18 15:47:42,122 [com.arjuna.ats.arjuna] TRACE - BasicAction::removeChildThread () action 0:ffffc0a80036:8073:5170078d:2 removing TSThread:1 result = true
2013-04-18 15:47:42,122 [com.arjuna.ats.arjuna] TRACE - TransactionReaper::remove ( BasicAction: 0:ffffc0a80036:8073:5170078d:2 status: ActionStatus.COMMITTED )

所有编辑都删除了客户详细信息,因此可能存在一些错误

非常感谢任何帮助!

最佳答案

对于现在发现此问题的任何人,我最终直接转到数据源并运行插入查询。

@Override
@Transactional
public void sessionCreated(HttpSessionEvent event) {
log.trace("sessionCreated(): "+event.getSession().getId());

JdbcTemplate select = new JdbcTemplate(getDS(event));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datetime = sdf.format(Calendar.getInstance().getTime());

select.update("INSERT INTO sessions (username, session, started) VALUES ('','"+event.getSession().getId()+"','"+datetime+"')");
}
private DataSource getDS(HttpSessionEvent event){
HttpSession session = event.getSession();

ApplicationContext ctx =
WebApplicationContextUtils.
getWebApplicationContext(session.getServletContext());

DataSource dao =
(DataSource) ctx.getBean("BPSCoreDS");

return dao;
}

关于java - HttpSessionListener 中的事务提交不会持久保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16086433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com