I am trying to issue a simple batchUpdate via JdbcTemplate on ORACLE database. I keep getting update return values as -2. I can complete the update via simple jdbc preparedStatement. But JdbcTemplate isn't able to do the same. What does the return value -2 from JdbcTemplate means anyway?
Related
I have a scanario where i have to make multiple stored procedure calls. If any one of the stored procedures fail, i have to roll back all the procedures.
May i please know how to achieve this using spring jdbc template. What i know is that i can call only one stored procedure using the spring jdbc template.
Is there any way to invoke a group of procedures in sequence using spring jdbc template?
One way of solving this problem is to create another new stored procedure and call all the procedures in this.
Is there any other efficient way to achieve this?
Following code will call multiple stored procedures within the same transaction.
#Transactional(rollbackFor=Exception.class)
public void callStoredProcedures(){
// Stored procedure 1
//....
// Stored procedure n
}
A transaction would be initialized at the method start. All the subsequent database calls within that method will take part in this transaction and any exception within the method context would rollback the transaction.
Note that the transaction rollback for this method is configured for any Exception. By default a transaction is marked for rollback on exceptions of type RuntimeException and JdbcTemplate methods throw DataAccessException which is its subclass. If no rollback is required for checked exceptions the (rollbackFor=Exception.class) can be removed.
Also , for #Transactional annotation to work , enable transaction management. Please go through #EnableTransactionManagement
I need to programmatically drop an EF core database. No problem, right? Get a context, call ctx.Database.EnsureDeleted(), and you're all set.
Except.. when the DB is in use, you're using connection pooling (there's good reason to do so), you can run into a situation where you can't drop the database.
Back in the EF6 days, I had a custom DB initializer which override InitializeDatabase as follows (setting the DB to single user mode so no other connections could interfere)
public override void InitializeDatabase(AudmDatabaseContext context)
{
context.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction
, string.Format("ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE", context.Database.Connection.Database));
base.InitializeDatabase(context);
}
Now I'm wondering.. how do you do this (or something else that gets me the same effect) using EF core 2.1?
I am new to Spring framework and trying to implement a simple CRUD application in spring boot with MySQL as database. Everything is working fine.
I have the Auto Increment enabled on Id field in the database. I am using EntityManager.persist() method to save the data in database and it is working fine. Now I want to return the auto generatedId back to the client as response of POST method but EntityManager.persist()return type is void.
Can anyone help me that how I can return the Id back?
the id is guaranteed after flush operation or when the transaction is completed.
em.persist(employee)
em.flush();
long id = employee.getId();
for more details read
What's the advantage of persist() vs save() in Hibernate?
I was wondering if there is a way for Flyway to accept an actual SQL migration as a string or a stream instead of searching for it on a classpath?
I'm constructing the SQL migration in Java on the fly and would like to call Flyway API and pass the migration as a paramter.
Please, let me know if this is possible.
Thank you
Not entirely what you are asking for, but looks like Java-based migrations might be a solution.
Basically instead of V1_0__script.sql you write V1_0__script.java class implementing JdbcMigration. Inside that class you have access to JDBC Connection:
class V1_0__script implements JdbcMigration {
public void migrate(Connection connection) throws Exception {
//...
}
}
In migrate() you are free to run your custom SQL queries.
There is no API available for this.
However, if you construct your SQL on the fly, it surely must be possible to construct it one statement at a time. Each statement can then be executed using the Connection parameter you get in a JdbcMigration
I am working on WCF Data Services. I have a function in my service which adds a record into a table in SQL DB and return the ID of the Inserted record.
I Used to Pass the values to be inserted in the form of Parameters to the Function.
For Example
public int Add(string Name, string Password)
{
// Here I will Add the record and return the ID of the record added in DB
}
But I dont want to do this way of passing in the form of parameters.
I want to pass the object directly.
Like
public int Add(User user)
{
// Here I will Add the record and return the ID of the record added in DB
}
I wrote the function like above in my services and my services project is successfull. When I update my ServiceRefrences I get Error.
It says only primitive types are supported. Is there any work around
this problem.
Thanks For Your Time in Answering my Question.
WCF Data Services (and OData protocol for that matter) only support passing primitive values as parameters to service operations.
The OData way of adding a new entity into an entity set is to send a POST with the entity in the payload to the entity set URL. In the WCF DS Service this will get translated into several calls to the IUpdatable interface on your context.