gpt4 book ai didi

Oracle Test Container throwing SQLSyntaxErrorException: ORA-01918: user does not exist

转载 作者:bug小助手 更新时间:2023-10-22 02:26:06 49 4
gpt4 key购买 nike



I am new to Oracle Container and I'm having a test container to open 2 database connections since I have 2 datasource in my spring boot application.

我是Oracle容器的新手,我有一个测试容器来打开2个数据库连接,因为我的Spring启动应用程序中有2个数据源。


This is the abstract class where I create Oracle container instances:

这是我创建Oracle容器实例的抽象类:


@Slf4j
public class AbstractContanierBaseTest {

static final OracleContainer ORACLE_OPIOWNER_CONTANINER;
static final OracleContainer ORACLE_FIRMAOWNER_CONTANINER;

static {
ORACLE_OPIOWNER_CONTANINER = new OracleContainer("gvenzl/oracle-xe:21-slim");
ORACLE_OPIOWNER_CONTANINER
.withUsername("opiowner")
.withPassword("password")
.withDatabaseName("OPIOWNER")
.withExposedPorts(1521)
.withLogConsumer(new Slf4jLogConsumer(log))
.withEnv("ORACLE_DATABASE", "OPIOWNER")
.withEnv("ORACLE_PASSWORD", "password")
.withEnv("APP_USER", "opiowner")
.withEnv("APP_USER_PASSWORD", "password")
.usingSid();
ORACLE_OPIOWNER_CONTANINER.start();

System.setProperty("spring.datasource.driver-class-name", ORACLE_OPIOWNER_CONTANINER.getDriverClassName());
System.setProperty("spring.datasource.url", ORACLE_OPIOWNER_CONTANINER.getJdbcUrl());
System.setProperty("spring.datasource.username", ORACLE_OPIOWNER_CONTANINER.getUsername());
System.setProperty("spring.datasource.password", ORACLE_OPIOWNER_CONTANINER.getPassword());

ORACLE_FIRMAOWNER_CONTANINER = new OracleContainer("gvenzl/oracle-xe:11-slim");
ORACLE_FIRMAOWNER_CONTANINER
.withUsername("firmaowner")
.withPassword("password")
.withDatabaseName("FIRMAOWNER")
.withLogConsumer(new Slf4jLogConsumer(log))
.withEnv("ORACLE_PASSWORD", "password")
.withEnv("APP_USER", "firmaowner")
.withEnv("APP_USER_PASSWORD", "password")
.usingSid();
;
ORACLE_FIRMAOWNER_CONTANINER.start();

System.setProperty("spring.second-datasource.url", ORACLE_FIRMAOWNER_CONTANINER.getJdbcUrl());
System.setProperty("spring.second-datasource.username", ORACLE_FIRMAOWNER_CONTANINER.getUsername());
System.setProperty("spring.second-datasource.password", ORACLE_FIRMAOWNER_CONTANINER.getPassword());

}

This class inherited by my integration tests written in junit5.

这个类由我用junit5编写的集成测试继承。


When executing junits an Sql script is launched before tests by means of @Sql annotation:

执行junits时,通过@ Sql annotation在测试前启动Sql脚本:


@Sql(config = @SqlConfig(transactionManager = "opiownerTransactionManager"), scripts = {"classpath:scripts/ddl/Recall_create_table_opiConfluenzaDati.sql", "classpath:scripts/dml/Recall_opiConfluenzaDati_script.sql"})

The script is executed properly, but I receive an oracle SqlSyntaxException as per Question title.

脚本执行正确,但我根据问题标题收到oracle SqlSyntaxException。


This is my create table script:

这是我的CREATE TABLE脚本:


CREATE TABLE opiowner.OPI_CONFLUENZA_DATI
(
"OPI_ANNO_EMISSIONE_TITOLO" NUMBER(4,0),
"OPI_TIPOLOGIADISPOSIZIONE" VARCHAR2(11),
"OPI_DATADISPOSIZIONE" DATE,
"OPI_ID_DISPOSIZIONE" NUMBER(10,0) NOT NULL,
"OPI_NUM_TITOLO" NUMBER(9,0),
"OPI_PROG_QUOTA" NUMBER(15,0),
"OPI_TIPO_COD_BENEF" VARCHAR2(2),
"OPI_CODICE_BENEF" VARCHAR2(16),
"OPI_TIPO_RECORD" VARCHAR2(11),
"OPI_SPECIE_TITOLO" VARCHAR2(2),
"OPI_TIPO_SPESA" VARCHAR2(5),
"OPI_COD_ENTE" VARCHAR2(15),
"OPI_IMPORTO" NUMBER(15,3),
"OPI_NOME_FILE" VARCHAR2(100),
"OPI_ORDINANTE" VARCHAR2(100)
);

ALTER TABLE opiowner.OPI_CONFLUENZA_DATI
ADD CONSTRAINT OPI_CONFLUENZA_DATI_PK PRIMARY KEY ("OPI_TIPOLOGIADISPOSIZIONE", "OPI_DATADISPOSIZIONE", "OPI_ID_DISPOSIZIONE");

As you may guess I need the 'opiowner' user to be present in the database. In particular in the first instance of the Oracle container: ORACLE_OPIOWNER firstly.

您可能猜到我需要“opiowner”用户存在于数据库中。特别是在Oracle容器的第一个实例中:首先是ORACLE_OPIOWNER。


Question: How can I create 'opiowner' user in a Oracle Container instance?

问题:如何在Oracle容器实例中创建“opiowner”用户?


I thought methods withUsername and withPassword would do the trick, but they do not...

我认为使用Username和withPassword的方法可以解决问题,但他们没有...


So as you can see with method withEnv I tried to override environment variables of the Docker image, but again no way...

正如您在withEnv方法中看到的,我尝试重写Docker镜像的环境变量,但还是没有办法……


How to fix this issue?

如何解决这个问题


更多回答

Hi. Create sql script init.sql with : CREATE USER OPIOWNER IDENTIFIED BY "password" QUOTA UNLIMITED ON USERS; GRANT CONNECT,RESOURCE TO OPIOWNER; Then on initialization of testcontainers add : ORACLE_OPIOWNER_CONTANINER.withInitScript("component/db/init.sql")

您好。使用以下内容创建SQL脚本init.sql : CREATE USER OPIOWNER IDENTIFIED BY "PASSWORD" QUOTE UNLIMITED ON USERS ; GRANT CONNECT , RESOURCE TO OPIOWNER ;然后在测试容器初始化时添加: ORACLE_OPIOWNER_CONTANINER.withInitScript ("component/db/init.sql")

Hi @zpavel, thanks for your attention. I tried and I get ORA-01031:insufficient privileges...

@ zpavel ,您好!感谢您的关注。我试了一下,得到了ORA-01031 :特权不足……

Sorry i coulent not ediit but it was : ORACLE_OPIOWNER_CONTANINER.withInitScript("init.sql"). So i suppose your init.sql was loaded as well (and you modified it). After which query it shows ora-01031 error?

对不起,我不是ediit ,但它是: ORACLE_OPIOWNER_CONTANINER.withInitScript ("init.sql")。所以我想你的init.sql也被加载了(你修改了它)。哪个查询后显示ora-01031错误?

@zpavel, Yes, correct.

@ zpavel ,是的,正确。

Please add the error in the question and also is there any way to reproduce the issue?

请在问题中添加错误,还有没有办法重现问题?

优秀答案推荐

I guess I found the issue that you are facing. Kindly go through this Github issue below:

我想我找到了您所面临的问题。请仔细阅读以下GitHub问题:


[OracleContainer] Unable to execute statements which need system privileges

[OracleContainer]无法执行需要系统权限的语句


Note: I'm not a DB Expert.

注意:我不是数据库专家。


Based on the information provided there, I've formed my answer which is as follows:

根据这里提供的信息,我的答案如下:



  • For an oracle-xe image, now the default user and schema is now "TEST".

    对于oracle-xe映像,现在默认的用户和架构现在是“TEST”。



  • I guess you won't be able to create any random user where you don't have the privileges. To overcome that, you have to use TEST user which is kind of acts as a super-user for test containers (Oracle Test containers uses/detects TEST as the default user).

    我猜你将无法在没有权限的情况下创建任何随机用户。为了克服这一点,您必须使用作为测试容器的超级用户的测试用户( Oracle测试容器使用/检测测试作为默认用户)。



  • So, what can I suggest is to create an init.sql some like this below:

    因此,我的建议是创建一个init.sql ,如下所示:


    CREATE USER OPIOWNER IDENTIFIED BY password;
    GRANT CONNECT TO OPIOWNER;
    GRANT SYSDBA TO OPIOWNER;
    GRANT CREATE SESSION GRANT ANY PRIVILEGE TO OPIOWNER;
    GRANT UNLIMITED TABLESPACE TO OPIOWNER;


  • Update AbstractContanierBaseTest class to some like this:

    将AbstractContanierBaseTest类更新为如下所示:


      @Slf4
    public class AbstractContanierBaseTest {

    static final OracleContainer ORACLE_OPIOWNER_CONTANINER;
    static final OracleContainer ORACLE_FIRMAOWNER_CONTANINER;

    static {
    ORACLE_OPIOWNER_CONTANINER = new OracleContainer("gvenzl/oracle-xe:21-slim");
    ORACLE_OPIOWNER_CONTANINER
    .withUsername("test")
    .withInitScript("init.sql");
    .usingSid();
    ORACLE_OPIOWNER_CONTANINER.start();

    System.setProperty("spring.datasource.driver-class-name", ORACLE_OPIOWNER_CONTANINER.getDriverClassName());
    System.setProperty("spring.datasource.url", ORACLE_OPIOWNER_CONTANINER.getJdbcUrl());
    System.setProperty("spring.datasource.username", ORACLE_OPIOWNER_CONTANINER.getUsername());
    System.setProperty("spring.datasource.password", ORACLE_OPIOWNER_CONTANINER.getPassword());

    ORACLE_FIRMAOWNER_CONTANINER = new OracleContainer("gvenzl/oracle-xe:11-slim").withCopyFileToContainer(MountableFile.forClasspathResource("init.sql"), "/container-entrypoint-startdb.d/init.sql");
    ORACLE_FIRMAOWNER_CONTANINER
    .withUsername("firmaowner")
    .withPassword("password")
    .withDatabaseName("FIRMAOWNER")
    .withLogConsumer(new Slf4jLogConsumer(log))
    .withEnv("ORACLE_PASSWORD", "password")
    .withEnv("APP_USER", "firmaowner")
    .withEnv("APP_USER_PASSWORD", "password")
    .usingSid();
    ;
    ORACLE_FIRMAOWNER_CONTANINER.start();

    System.setProperty("spring.second-datasource.url", ORACLE_FIRMAOWNER_CONTANINER.getJdbcUrl());
    System.setProperty("spring.second-datasource.username", ORACLE_FIRMAOWNER_CONTANINER.getUsername());
    System.setProperty("spring.second-datasource.password", ORACLE_FIRMAOWNER_CONTANINER.getPassword());

    }


  • Now, update the create table script to this:

    现在,将CREATE TABLE脚本更新为:


      ALTER SESSION SET CONTAINER=<<NAME OF TEST CONTAINER>>;

    CREATE TABLE TEST.OPI_CONFLUENZA_DATI
    (
    "OPI_ANNO_EMISSIONE_TITOLO" NUMBER(4,0),
    "OPI_TIPOLOGIADISPOSIZIONE" VARCHAR2(11),
    "OPI_DATADISPOSIZIONE" DATE,
    "OPI_ID_DISPOSIZIONE" NUMBER(10,0) NOT NULL,
    "OPI_NUM_TITOLO" NUMBER(9,0),
    "OPI_PROG_QUOTA" NUMBER(15,0),
    "OPI_TIPO_COD_BENEF" VARCHAR2(2),
    "OPI_CODICE_BENEF" VARCHAR2(16),
    "OPI_TIPO_RECORD" VARCHAR2(11),
    "OPI_SPECIE_TITOLO" VARCHAR2(2),
    "OPI_TIPO_SPESA" VARCHAR2(5),
    "OPI_COD_ENTE" VARCHAR2(15),
    "OPI_IMPORTO" NUMBER(15,3),
    "OPI_NOME_FILE" VARCHAR2(100),
    "OPI_ORDINANTE" VARCHAR2(100)
    );

    ALTER TABLE TEST.OPI_CONFLUENZA_DATI
    ADD CONSTRAINT OPI_CONFLUENZA_DATI_PK PRIMARY KEY ("OPI_TIPOLOGIADISPOSIZIONE", "OPI_DATADISPOSIZIONE", "OPI_ID_DISPOSIZIONE");



See if this changes work. That's all. Look at that github issue for more info.

查看此更改是否可行。仅此而已。有关更多信息,请查看github问题。


更多回答

Dear @Anish, your answer makes arise a doubt in my mind. In the code under test, entities are defined with schemas. So when a query Is done in a table of firmaowner user hibernate adds the user before any select or insert. I think, as I tried, that if i use the tecnique you have suggested, the same exceprion User does not exist Will be thrown... Indeed, I tried to create the tables without indicating a user, and the tables have bene created, but the exceprion Is thrown

亲爱的@ Anish ,你的回答让我产生怀疑。在被测试的代码中,实体使用架构定义。因此,当在firmaowner表中完成查询时,用户hibernate会在任何SELECT或INSERT之前添加用户。我认为,正如我所尝试的那样,如果我使用您建议的技术,相同的exceprion User不存在将被抛出......确实,我尝试在没有指示用户的情况下创建表,并且表已经创建,但是exceprion被抛出

@SaverioMirkoViola First of all, are you able to create any user? Because you said that you were not able to create user right?

@ SaverioMirkoViola首先,你能创建任何用户吗?因为你说你无法创建用户,对吗?

@SaverioMirkoViola First try with 1 datasource and execute the query, see what issues are you facing?

@ SaverioMirkoViola首先尝试使用1个数据源并执行查询,看看您遇到了什么问题?

Hi @Anish, I'm not able to execute the script because of error Sql = CONNECT TEST/password@OPIOWNER, OriginalSql = CONNECT TEST/password@OPIOWNER, Error Msg = ORA-00900: invalid sql statement. Anyway, thing is that I need to create the table for OPIOWNER user and not for TEST. I am not able to create any user...

@ Anish ,您好!由于错误Sql = CONNECT TEST/PASSWORD @ OPIOWNER , OriginalSql = CONNECT TEST/PASSWORD @ OPIOWNER , Error Msg = ORA-00900: invalid sql statement ,我无法执行脚本。无论如何,我需要为OPIOWNER用户创建表,而不是为了测试。我无法创建任何用户...

Could you please explain to me how can create OPIOWNER user by means of TEST?

您能否向我解释一下如何通过测试创建OPIOWNER用户?

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