我最近开始使用JBossTM以便在多个数据库连接上使用JTA,这非常成功。如果我尝试从任何控制器执行事务,它可以正常工作。
但是,我最近开始尝试从HttpSessionListener的实例中保留实体,以便我可以跟踪用户会话,但我无法使持久性工作。我确信这里有一些非常简单的东西,但我会非常欢迎任何提示。
问题在于,虽然一切似乎都运行正常(日志明智)但实际上并没有提交到数据库。我在尝试持久化到没有事务管理器的数据库之前看到过这个错误(在我切换到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' />
的web.xml
<listener>
<listener-class>core.util.SessionListener</listener-class>
</listener>
SessionListener.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;
}
}
的persistence.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 )
全部编辑以删除客户端详细信息,因此可能存在一些错误
感谢任何帮助!
答案 0 :(得分:0)
对于现在发现这一点的人,我最终直接进入数据源并运行插入查询。
@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;
}