Unable to persist Database Using Pgpool (Vertx Reactive Postgres Client)

41 Views Asked by At

I'm working on a project to test vertex Integration with Spring boot and persisting database using Pgpool, integration is successful but my Postgres database does not persist when I test the endpoint on Postman, it returns a JsonObject of the added employee but no changes are made in the database Here is what I have done

MainVerticle

public class MainVerticle extends AbstractVerticle {

    private final static Logger LOGGER = Logger.getLogger(MainVerticle.class.getName());
    private final Router router;
    private final Integer serverPort;

    @Autowired
    public MainVerticle(Router router, @Value("${vertx.spring.port}") Integer serverPort) {
        this.router = router;
        this.serverPort = serverPort;
    }

    @Override
    public Uni<Void> asyncStart() {
        return vertx.createHttpServer()
                .requestHandler(router)
                .listen(serverPort)
                .onItem()
                .invoke(s -> LOGGER.log(Level.INFO, "Server Running on http://localhost:" + s.actualPort()))
                .onFailure()
                .invoke(t -> LOGGER.log(Level.SEVERE, t.getMessage()))
                .replaceWithVoid();
    }

}

DatabaseConfig

public class DatabaseConfig {

    @Bean
    @Autowired
    public PgPool pgPool(Vertx vertx) {
        PgConnectOptions options = new PgConnectOptions()
                .setUser("postgres")
                .setPassword("password")
                .setDatabase("postgres")
                .setPort(5432);

        PoolOptions poolOptions = new PoolOptions()
                .setMaxSize(5);
        return PgPool.pool(vertx, options, poolOptions);
    }

}

AddEmployee

public class CreateEmployeeController implements Consumer<RoutingContext> {

    private static final Logger LOGGER = LoggerFactory.getLogger(CreateEmployeeController.class);

    private final PgPool pgPool;

    public CreateEmployeeController(Router router, PgPool pgPool) {
        this.pgPool = pgPool;
        router.post("/addemployee")

                .handler(BodyHandler.create())
                .handler(this);
    }

    @Override
    public void accept(RoutingContext ctx) {
        JsonObject jsonObject = ctx.body().asJsonObject();
        Employee emp = jsonObject.mapTo(Employee.class);


         pgPool.withConnection(conn -> conn.prepare("INSERT INTO Employee (name, department, salary) VALUES ($1, $2, $3) return Id "))
                .flatMap(preparedStatement -> preparedStatement
                        .query()
                        .execute(Tuple.of(emp.getName(), emp.getDepartment(), emp.getSalary()))
                        .map(RowSet::iterator)
                        .flatMap(rowRowIterator -> {
                            if (rowRowIterator.hasNext()) {
                                Row row = rowRowIterator.next();
                                return Uni.createFrom().item("added successfully");
                            } else {
                                return Uni.createFrom().failure(new RuntimeException("Error: Unable to insert record!"));
                            }
                        }));

        ctx.response()
                .putHeader("content-type", "application/json")
                .end(jsonObject.encode()).subscribeAsCompletionStage();
    }
}
0

There are 0 best solutions below