Mybatis SQL 会话提交似乎比下面的代码慢
Mybatis SQL session commit seemingly slower than following code
背景
我们在 Java 中编写了 2 个服务 - 一个处理不同文件的数据库操作(数据库上的 CRUD),另一个处理这些记录的长期 运行 处理(复杂的后台任务)。简单来说,我们可以说他们是生产者和消费者。
假设行为如下:
服务 1(使用以下代码):
将文件存入数据库
如果文件是'C'类型则放入消息队列进一步处理
服务 2:
从消息队列接收消息
从数据库加载文件(按ID)
执行进一步处理
Service 1的代码如下(公司原因改了一些名字)
private void persist() throws Exception {
try (SqlSession sqlSession = sessionFactory.openSession()) {
FileType fileType = FileType.fromFileName(filename);
FileEntity dto = new FileEntity(filename, currentTime(), null, user.getName(), count, data);
oracleFileStore.create(sqlSession, dto);
auditLog.logFileUploaded(user, filename, count);
sqlSession.commit();
if (fileType == FileType.C) {
mqClient.submit(new Record(dto.getId(), dto.getName(), user));
auditLog.logCFileDetected(user, filename);
}
}
}
附加信息
消息队列使用ActiveMQ 5.15
数据库是 Oracle 12c
数据库由Mybatis 3.4.1处理
问题
服务 2 有时会收到来自 MQ 的消息,尝试从数据库中读取文件,但令人惊讶的是 - 文件不存在。这种事件非常罕见,但确实发生了。当我们检查数据库时,文件就在那里。几乎看起来文件的后台处理在文件被放入数据库之前就开始了。
问题
MQ 调用是否可能比数据库提交更快?我在 DB 中创建了名为 commit 的文件,然后才将消息放入 MQ。 MQ甚至包含数据库本身生成的ID(序列)。
是否需要关闭连接以确保提交已执行?我一直以为当我提交时,无论我的事务是否结束,它都在数据库中。
会不会是Mybatis的问题?我已经阅读了一些关于 Mybatis transactions/sessions 的问题,但它似乎与我的问题不相似
更新
我可以提供一些额外的代码,但请理解,出于公司原因我无法分享所有内容。如果你没有看到任何明显的东西,那很好。不幸的是,我无法继续进行比这更深入的分析。
另外我主要是想确认一下我对SQL和Mybatis的理解是否正确,我也可以把这样的回复标记为正确。
SessionFactory.java(摘录)
private SqlSessionFactory createLegacySessionFactory(DataSource dataSource) throws Exception
{
Configuration configuration = prepareConfiguration(dataSource);
return new SqlSessionFactoryBuilder().build(configuration);
}
//javax.sql.DataSource
private Configuration prepareConfiguration(DataSource dataSource)
{
//classes from package org.apache.ibatis
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
addSettings(configuration);
addTypeAliases(configuration);
addTypeHandlers(configuration);
configuration.addMapper(PermissionMapper.class);
addMapperXMLs(configuration); //just add all the XML mappers
return configuration;
}
public SqlSession openSession()
{
//Initialization of factory is above
return new ForceCommitSqlSession(factory.openSession());
}
ForceCommitSqlSession.java(摘录)
/**
* ForceCommitSqlSession is wrapper around mybatis {@link SqlSession}.
* <p>
* Its purpose is to force commit/rollback during standard commit/rollback operations. The default implementation (according to javadoc)
* does
* not commit/rollback if there were no changes to the database - this can lead to problems, when operations are executed outside mybatis
* session (e.g. via {@link #getConnection()}).
*/
public class ForceCommitSqlSession implements SqlSession
{
private final SqlSession session;
/**
* Force the commit all the time (despite "generic contract")
*/
@Override
public void commit()
{
session.commit(true);
}
/**
* Force the roll back all the time (despite "generic contract")
*/
@Override
public void rollback()
{
session.rollback(true);
}
@Override
public int insert(String statement)
{
return session.insert(statement);
}
....
}
OracleFileStore.java(摘录)
public int create(SqlSession session, FileEntity fileEntity) throws Exception
{
//the mybatis xml is simple insert SQL query
return session.insert(STATEMENT_CREATE, fileEntity);
}
Is it possible that MQ call could be faster than the database commit?
如果数据库提交完成,则更改在数据库中。队列中任务的创建发生在这之后。这里最主要的是,当您在会话中调用 commit
时,您需要检查提交是否同步发生。从您目前提供的配置来看,它似乎没问题,除非 Connection
本身存在一些问题。例如,我可以想象原生 Connection
上有一些包装器。我会在调试器中检查 commit
调用导致对来自 oracle JDBC 驱动程序的实现调用 Connection.commit
。最好在DB端查看日志。
Does the connection needs to be closed to be sure the commit was performed? I always thought when I commit then it's in the database regardless if my transaction ended or not.
你是对的。无需关闭遵守 JDBC 规范的连接(本机 JDCB 连接会这样做)。因为你总是可以创建一些不遵守 Connection
API 并做一些魔术的包装器(比如延迟提交直到连接关闭)。
Can the problem be Mybatis? I've read some problems regarding Mybatis transactions/sessions but it doesn't seem similar to my problem
我会说这不太可能。您正在使用 JdbcTransactionFactory
确实提交给数据库。您需要跟踪 commit
上发生的事情才能确定。
你检查过问题不在reader这边吗?例如它可能使用具有序列化隔离级别的长事务,在这种情况下它无法读取数据库中的更改。
在 postgres 中,如果使用复制并将副本用于读取查询 reader,即使在主服务器上成功完成提交,也可能会看到过时的数据。我对 oracle 不太熟悉,但 it seems 如果使用复制,你可能会遇到同样的问题:
A table snapshot is a transaction-consistent reflection of its master data as that data existed at a specific point in time. To keep a snapshot's data relatively current with the data of its master, Oracle must periodically refresh the snapshot
我会检查数据库的设置以了解是否属于这种情况。如果使用复制,你需要改变你的方法。
背景
我们在 Java 中编写了 2 个服务 - 一个处理不同文件的数据库操作(数据库上的 CRUD),另一个处理这些记录的长期 运行 处理(复杂的后台任务)。简单来说,我们可以说他们是生产者和消费者。
假设行为如下:
服务 1(使用以下代码):
将文件存入数据库
如果文件是'C'类型则放入消息队列进一步处理
服务 2:
从消息队列接收消息
从数据库加载文件(按ID)
执行进一步处理
Service 1的代码如下(公司原因改了一些名字)
private void persist() throws Exception {
try (SqlSession sqlSession = sessionFactory.openSession()) {
FileType fileType = FileType.fromFileName(filename);
FileEntity dto = new FileEntity(filename, currentTime(), null, user.getName(), count, data);
oracleFileStore.create(sqlSession, dto);
auditLog.logFileUploaded(user, filename, count);
sqlSession.commit();
if (fileType == FileType.C) {
mqClient.submit(new Record(dto.getId(), dto.getName(), user));
auditLog.logCFileDetected(user, filename);
}
}
}
附加信息
消息队列使用ActiveMQ 5.15
数据库是 Oracle 12c
数据库由Mybatis 3.4.1处理
问题
服务 2 有时会收到来自 MQ 的消息,尝试从数据库中读取文件,但令人惊讶的是 - 文件不存在。这种事件非常罕见,但确实发生了。当我们检查数据库时,文件就在那里。几乎看起来文件的后台处理在文件被放入数据库之前就开始了。
问题
MQ 调用是否可能比数据库提交更快?我在 DB 中创建了名为 commit 的文件,然后才将消息放入 MQ。 MQ甚至包含数据库本身生成的ID(序列)。
是否需要关闭连接以确保提交已执行?我一直以为当我提交时,无论我的事务是否结束,它都在数据库中。
会不会是Mybatis的问题?我已经阅读了一些关于 Mybatis transactions/sessions 的问题,但它似乎与我的问题不相似
更新
我可以提供一些额外的代码,但请理解,出于公司原因我无法分享所有内容。如果你没有看到任何明显的东西,那很好。不幸的是,我无法继续进行比这更深入的分析。
另外我主要是想确认一下我对SQL和Mybatis的理解是否正确,我也可以把这样的回复标记为正确。
SessionFactory.java(摘录)
private SqlSessionFactory createLegacySessionFactory(DataSource dataSource) throws Exception
{
Configuration configuration = prepareConfiguration(dataSource);
return new SqlSessionFactoryBuilder().build(configuration);
}
//javax.sql.DataSource
private Configuration prepareConfiguration(DataSource dataSource)
{
//classes from package org.apache.ibatis
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
addSettings(configuration);
addTypeAliases(configuration);
addTypeHandlers(configuration);
configuration.addMapper(PermissionMapper.class);
addMapperXMLs(configuration); //just add all the XML mappers
return configuration;
}
public SqlSession openSession()
{
//Initialization of factory is above
return new ForceCommitSqlSession(factory.openSession());
}
ForceCommitSqlSession.java(摘录)
/**
* ForceCommitSqlSession is wrapper around mybatis {@link SqlSession}.
* <p>
* Its purpose is to force commit/rollback during standard commit/rollback operations. The default implementation (according to javadoc)
* does
* not commit/rollback if there were no changes to the database - this can lead to problems, when operations are executed outside mybatis
* session (e.g. via {@link #getConnection()}).
*/
public class ForceCommitSqlSession implements SqlSession
{
private final SqlSession session;
/**
* Force the commit all the time (despite "generic contract")
*/
@Override
public void commit()
{
session.commit(true);
}
/**
* Force the roll back all the time (despite "generic contract")
*/
@Override
public void rollback()
{
session.rollback(true);
}
@Override
public int insert(String statement)
{
return session.insert(statement);
}
....
}
OracleFileStore.java(摘录)
public int create(SqlSession session, FileEntity fileEntity) throws Exception
{
//the mybatis xml is simple insert SQL query
return session.insert(STATEMENT_CREATE, fileEntity);
}
Is it possible that MQ call could be faster than the database commit?
如果数据库提交完成,则更改在数据库中。队列中任务的创建发生在这之后。这里最主要的是,当您在会话中调用 commit
时,您需要检查提交是否同步发生。从您目前提供的配置来看,它似乎没问题,除非 Connection
本身存在一些问题。例如,我可以想象原生 Connection
上有一些包装器。我会在调试器中检查 commit
调用导致对来自 oracle JDBC 驱动程序的实现调用 Connection.commit
。最好在DB端查看日志。
Does the connection needs to be closed to be sure the commit was performed? I always thought when I commit then it's in the database regardless if my transaction ended or not.
你是对的。无需关闭遵守 JDBC 规范的连接(本机 JDCB 连接会这样做)。因为你总是可以创建一些不遵守 Connection
API 并做一些魔术的包装器(比如延迟提交直到连接关闭)。
Can the problem be Mybatis? I've read some problems regarding Mybatis transactions/sessions but it doesn't seem similar to my problem
我会说这不太可能。您正在使用 JdbcTransactionFactory
确实提交给数据库。您需要跟踪 commit
上发生的事情才能确定。
你检查过问题不在reader这边吗?例如它可能使用具有序列化隔离级别的长事务,在这种情况下它无法读取数据库中的更改。
在 postgres 中,如果使用复制并将副本用于读取查询 reader,即使在主服务器上成功完成提交,也可能会看到过时的数据。我对 oracle 不太熟悉,但 it seems 如果使用复制,你可能会遇到同样的问题:
A table snapshot is a transaction-consistent reflection of its master data as that data existed at a specific point in time. To keep a snapshot's data relatively current with the data of its master, Oracle must periodically refresh the snapshot
我会检查数据库的设置以了解是否属于这种情况。如果使用复制,你需要改变你的方法。