当前位置: 首页 > 知识库问答 >
问题:

使用Informix数据库调用Spring Boot Rest API时出现“java.sql.SQLException:不支持事务”错误

窦华晖
2023-03-14

我创建了一个简单的Spring boot Rest应用程序,可以将数据获取/发布到Informix数据库。

在调用Rest API获取下面提到的错误消息时,我搜索了相同的根本原因,但没有找到任何来源,

感谢对此的帮助,这是我创建的配置和代码:

应用属性

spring.datasource.url=jdbc:informix-sqli://XXXX:XXXX/localdb:INFORMIXSERVER=ol_xxxxx
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=com.informix.jdbc.IfxDriver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.InformixDialect
spring.jpa.hibernate.ddl-auto=update

连同Spring starter Web、Data JPA、开发工具一起添加到附加依赖项下方

   <dependency>
        <groupId>com.ibm.informix</groupId>
        <artifactId>jdbc</artifactId>
        <version>4.10.8.1</version>
    </dependency>

Rest控制器

@RestController
@RequestMapping("/api")
public class UserController {
    @Autowired
    UserRepository repository;
    
    @GetMapping("/user/{userId}")
    public void getUserDetails(@PathVariable Long countryId) {
      Optional<webCountry> country = repository.findById(countryId);
      System.out.println(country);
    }
    
    @PostMapping(path="/saveuser")
    public void saveUser(@RequestBody UserEntity user) {
        repository.save(data);
    }
}

存储库界面

@Repository
public interface UserRepository extends JpaRepository<UserEntity, Long> {

}

从post Man调用Get API时得到低于错误消息

    Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.TransactionException: JDBC begin transaction failed: ] with root cause

java.sql.SQLException: Transactions not supported
    at com.informix.util.IfxErrMsg.buildException(IfxErrMsg.java:474) ~[jdbc-4.10.8.1.jar:4.10.8.1]
    at com.informix.util.IfxErrMsg.getSQLException(IfxErrMsg.java:443) ~[jdbc-4.10.8.1.jar:4.10.8.1]
    at com.informix.util.IfxErrMsg.getSQLException(IfxErrMsg.java:394) ~[jdbc-4.10.8.1.jar:4.10.8.1]
    at com.informix.jdbc.IfxSqliConnect.setAutoCommit(IfxSqliConnect.java:2426) ~[jdbc-4.10.8.1.jar:4.10.8.1]
    at com.zaxxer.hikari.pool.ProxyConnection.setAutoCommit(ProxyConnection.java:414) ~[HikariCP-3.4.5.jar:na]
    at com.zaxxer.hikari.pool.HikariProxyConnection.setAutoCommit(HikariProxyConnection.java) ~[HikariCP-3.4.5.jar:na]
    at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.begin(AbstractLogicalConnectionImplementor.java:72) ~[hibernate-core-5.4.25.Final.jar:5.4.25.Final]
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.begin(LogicalConnectionManagedImpl.java:282) ~[hibernate-core-5.4.25.Final.jar:5.4.25.Final]
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.begin(JdbcResourceLocalTransactionCoordinatorImpl.java:246) ~[hibernate-core-5.4.25.Final.jar:5.4.25.Final]
    at org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:83) ~[hibernate-core-5.4.25.Final.jar:5.4.25.Final]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.beginTransaction(HibernateJpaDialect.java:184) ~[spring-orm-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:402) ~[spring-orm-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.startTransaction(AbstractPlatformTransactionManager.java:400) ~[spring-tx-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373) ~[spring-tx-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:574) ~[spring-tx-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:361) ~[spring-tx-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118) ~[spring-tx-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:178) ~[spring-data-jpa-2.3.6.RELEASE.jar:2.3.6.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95) ~[spring-aop-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.12.RELEASE.jar:5.2.12.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.2.12.RELEASE.jar:5.2.12.RELEASE]

共有3个答案

谢学名
2023-03-14

您可以尝试向存储库中添加@事务性注释。参考:https://www.baeldung.com/transaction-configuration-with-jpa-and-spring

松刚豪
2023-03-14

Informix提供了四种不同的数据库类型:

  • 未登录
  • 缓冲日志
  • 无缓冲日志
  • 无缓冲日志记录模式ANSI

未标记的数据库没有事务日志,因此不支持事务。所有其他类型都支持事务;品种之间的差异现在已经无关紧要了。

当您说创建数据库xyz时,默认的数据库类型是未标记的数据库。您必须使用日志创建数据库,或使用缓冲日志创建数据库,或使用日志模式ANSI创建数据库。

从错误消息判断,您正在使用的数据库是在没有事务的情况下创建的。

如有必要(而且可能是必要的),DBSA(数据库系统管理员-通常,这意味着有权作为用户informix工作的人)可以更改日志记录模式。切换到缓冲日志记录可能是合适的。DBSA可以在执行服务器备份时使用ontape更改模式,也可以使用ondblog(但即便如此,备份也是一个好主意):

ontape -s -B xyz

ondblog buf xyz

有关详细信息,请参阅管理数据库日志记录模式。

尝试在未标记的Informix数据库中启动事务时,生成的错误消息为:

-256 Transaction not available.

Informix finderr命令显示:

$ finderr -256
-256    Transaction not available.

You cannot begin a transaction in this database because it does not
have a transaction log. In order to support transactions, you must
start a transaction log. Refer to your Administrator's Guide for
information on how to start a transaction log.

$
夏才
2023-03-14

这可能有以下一个或多个原因:

  • JDBC URL中的IP地址或主机名错误
  • 本地DNS服务器无法识别JDBC URL中的主机名
  • JDBC URL中缺少端口号或端口号错误
  • DB服务器已关闭
  • DB server不接受TCP/IP连接
  • DB服务器的连接已用完
 类似资料:
  • 问题内容: 运行removeUserFromConference方法时,遇到以下异常: 道方法: 模型类: 问题答案: 您可能已经开始了一笔交易,并尝试开始另一笔交易而没有提交或回滚上一笔交易。使用程序化事务划分时的惯用法如下: 这很麻烦且容易出错,这就是为什么使用EJB或Spring具有声明式事务如此有用的原因之一。

  • 我们继续上一章节的内容,大家应该记得我们 Lua 代码中是如何完成 ngx_postgres 模块调用的。我们把他简单改造一下,让他更接近真实代码。 local json = require "cjson" function db_exec(sql_str) local res = ngx.location.capture('/postgres',

  • 最近我实现了一个新的构建器与gradle运行詹金斯。我们的项目包含jasper报告,所以我在下面添加了一个由jasper报告api提供的蚂蚁任务来编译jasper报告。 一切都很好,编译和预期的工作,但在部署项目到服务器后,在运行时得到以下错误。 [8/3/17 14:38:53:340 EET] 000000f5 SystemOut O ERROR[14:38:53,333-WebContain

  • 我正在建立一个应用程序,使用蓝牙支持。在添加libs并按此处的指示刷新https://github.com/chen-fishbein/bluetoothle-codenameone之后,下面是错误日志 C:\users\ruffcode\document\mpayment\src\userclasses\statemachine.java:359:错误:找不到符号私有蓝牙BT;符号:Blueto

  • 我正在尝试使用Java JTDS驱动程序连接到Scala中的数据库。然而,每当我尝试使用它时,我会得到一个错误,即(Java的?)是错的。 java.lang.unsupportedClassVersionError:net/sourceforge/jtds/jdbcx/jtdsDataSource:Unsupported major.minor版本51.0 null java版本“1.6.0_3

  • MongoDB 4.0将引入具有ACID保证的事务支持。 Spring Data MongoDB是否已经支持MongoDB中的事务,如果不是,那么这个很棒的功能何时可用。我真的需要它,考虑到以下问题 - MongoDB模式设计,以支持应用程序水平扩展