Getting oracle table row lock, from application - oracle

I am getting oracle table row lock, while using below mentioned method written in DAOImpl only in production server. Its working fine in testing server.
`public boolean inactiveServices(String custUid) {
Session session = null;
try {
session = sessionFactory.openSession();
Long count = (Long) (session.createQuery(
"select count(*) as intcustomeridpk from CustomerRegistrationPOJO t where t.customerRegistrationPK.custUid = '"
+ custUid + "'").iterate().next());
if (count < 1)
return false;
String queryToFetchUserIdPk = "select t.customerRegistrationPK.intCustomerId as intcustomeridpk from CustomerRegistrationPOJO t where t.customerRegistrationPK.custUid = '"
+ custUid + "'";
//Iterator iterator = session.createQuery(queryToFetchUserIdPk).iterate();
String currentCustIntIdPk = (String) session.createQuery(queryToFetchUserIdPk).iterate().next();
System.out.println("currentCustIntIdPk "+currentCustIntIdPk);
Criteria criteria = session.createCriteria(CustUtilsPOJO.class).add(
Restrictions.eq("custUtlilityKey.custUid", currentCustIntIdPk))
.add(Restrictions.eq("ynActive", "Y"))
.add(Restrictions.ne("dmlFlag", 2));
List<CustUtilsPOJO> foundUtilList = criteria.list();
for (CustUtilsPOJO eachUtil : foundUtilList) {
System.out.println("centerCode=="+eachUtil.getCenterCode()+"serviceId=="+eachUtil.getCustUtlilityKey().getUtilNumber()+"utilityId=="+eachUtil.getCustUtlilityKey().getUtilId()+"intCustUtil=="+eachUtil.getIntCustomerUtlServiceId());
eachUtil.setDmlFlag(2);
eachUtil.setYnActive("N");
session.merge(eachUtil);
}
Transaction txn = session.beginTransaction();
txn.commit();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (session != null && session.isOpen()) {
session.flush();
session.clear();
session.close();
}
}
return true;
}`

Related

Domino/Notes nhttp spikes on JDBC query to Informix

When connecting to an Informix server via a Bean (or Jar - I've tried both) in an NSF, the nhttp task it spiking and staying spiked. I do not get this problem if the code is run in Eclipse. Here is what I trigger from an XPage (via a managed bean):
public void InformixSQLRS() {
try {
String url = "jdbc:informix-sqli://IPADDRESSHERE:PORTHERE/db_cra:INFORMIXSERVER=SERVERNAME;user=USERNAME;password=PASSWORD";
Class.forName("com.informix.jdbc.IfxDriver");
conn = DriverManager.getConnection(url);
System.out.println("After getting Driver");
String selectSQL = "SELECT FIRST 10 * FROM RtCSQsSummary ";
String whereSQL = "WHERE startdatetime >= TODAY ";
String orderbySQL = "ORDER BY startdatetime DESC";
String strsql = selectSQL + orderbySQL;
stmt = conn.createStatement();
System.out.println(strsql);
ResultSet rs = stmt.executeQuery(strsql);
System.out.println("after executeQuery");
ResultSetMetaData rsmd = rs.getMetaData();
int ncols = rsmd.getColumnCount();
int i, type;
String s = null;
for (i = 1; i < ncols; i++) {
System.out.println(
rsmd.getColumnLabel(i) + " " + rsmd.getColumnType(i));
}
stmt.close();
rs.close();
conn.close();
} catch (
SQLException e) {
System.out.println("ERROR: failed to connect!");
System.out.println("ERROR: " + e.getMessage());
e.printStackTrace();
return;
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException se) {
}
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se) {
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
}
System.out.println("Goodbye! (finally)");
}
System.out.println("Last call");
}
I am on DDE Release 9.0.1FP10 SHF81. I have the Informix JDBC driver jdbc-4.10.8.1.jar. I have tried putting it in the Jars element, and importing to WebContent/WEB_INF/lib. This happens on both a Domino server Release 9.0.1FP9HF63 on Linux 2.6.32-696.13.2.el6.x86_64 and my local running Windows 7.
I get this error: Exception in thread "Informix-PreparedStatementCacheCleanupThread". Debugged in DDE and found that "IfxSqliConnectCleaner-Thread-1" was taking up a lot of CPU. Suspending that thread let the CPU count to return to normal.
The process completes, printing the results, and the strings at the end of finally and the block. Closing the browser does not release nhttp.
This matches samples provided to connect to Informix. I'm not sure what is causing it to spike/peg for Domino. Is there something I can do to get the thread to release?

ResultSet doesn't return values for DB2, but it return values if I try to do it manualy

I'm trying to get values from resulset, but it return nothing.
When i'm trying to do it through plain sql it return some values.
List<String> res = new ArrayList<String>();
try {
String query = "SELECT COLUMN_NAME FROM idoc.columns_to_show where user = ? "
+ DAO.DB2_UR_POSTFIX;
Connection connection = Properties.getDocsConnection();
try {
PreparedStatement pr = connection.prepareStatement(query);
try {
pr.setString(1, user.getDomainName());
ResultSet rs = pr.executeQuery();
try {
while (rs.next()) {
res.add(rs.getString("COLUMN_NAME"));
}
} finally {
rs.close();
}
} finally {
pr.close();
}
} finally {
connection.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return res;
Don't use column name "user" it is reserved name for DB2 database.
Therefore I couldn't find any result.

How to call Oracle function from Hibernate3 [duplicate]

My question is very much like Getting the return value of a PL/SQL function via Hibernate
I have a function which does some modifications internally and it returns a value.
The original idea was to do something like this:
protected Integer checkXXX(Long id, Long transId)
throws Exception {
final String sql = "SELECT MYSCHEMA.MYFUNC(" + id + ", "
+ transId + ") FROM DUAL";
final BigDecimal nr = (BigDecimal) this.getHibernateTemplate()
.getSessionFactory().getCurrentSession().createSQLQuery(sql)
.uniqueResult();
return nr.intValue();
}
Unfortunately this doesn't work with Oracle. What is the recommended way to do something like this?
Is there a way to extract declared variables from within my statement?
Hibernate Session provides a doWork() method that gives you direct access to java.sql.Connection. You can then create and use java.sql.CallableStatement to execute your function:
session.doWork(new Work() {
public void execute(Connection connection) throws SQLException {
CallableStatement call = connection.prepareCall("{ ? = call MYSCHEMA.MYFUNC(?,?) }");
call.registerOutParameter( 1, Types.INTEGER ); // or whatever it is
call.setLong(2, id);
call.setLong(3, transId);
call.execute();
int result = call.getInt(1); // propagate this back to enclosing class
}
});
You have the following options:
With a #NamedNativeQuery:
#org.hibernate.annotations.NamedNativeQuery(
name = "fn_my_func",
query = "{ ? = call MYSCHEMA.MYFUNC(?, ?) }",
callable = true,
resultClass = Integer.class
)
Integer result = (Integer) entityManager.createNamedQuery("fn_my_func")
.setParameter(1, 1)
.setParameter(2, 1)
.getSingleResult();
With JDBC API:
Session session = entityManager.unwrap( Session.class );
final AtomicReference<Integer> result =
new AtomicReference<>();
session.doWork( connection -> {
try (CallableStatement function = connection
.prepareCall(
"{ ? = call MYSCHEMA.MYFUNC(?, ?) }"
)
) {
function.registerOutParameter( 1, Types.INTEGER );
function.setInt( 2, 1 );
function.setInt( 3, 1 );
function.execute();
result.set( function.getInt( 1 ) );
}
} );
With a native Oracle query:
Integer result = (Integer) entityManager.createNativeQuery(
"SELECT MYSCHEMA.MYFUNC(:postId, :transId) FROM DUAL")
.setParameter("postId", 1)
.setParameter("transId", 1)
.getSingleResult();
Yes, you do need to use an out parameter. If you use the doWork() method, you'd do something like this:
session.doWork(new Work() {
public void execute(Connection conn) {
CallableStatement stmt = conn.prepareCall("? = call <some function name>(?)");
stmt.registerOutParameter(1, OracleTypes.INTEGER);
stmt.setInt(2, <some value>);
stmt.execute();
Integer outputValue = stmt.getInt(1);
// And then you'd do something with this outputValue
}
});
Alternative code :)
if you want to direct result you can use below code
int result = session.doReturningWork(new ReturningWork<Integer>() {
#Override
public Integer execute(Connection connection) throws SQLException {
CallableStatement call = connection.prepareCall("{ ? = call MYSCHEMA.MYFUNC(?,?) }");
call.registerOutParameter( 1, Types.INTEGER ); // or whatever it is
call.setLong(2, id);
call.setLong(3, transId);
call.execute();
return call.getInt(1); // propagate this back to enclosing class
}
});
http://keyurj.blogspot.com.tr/2012/12/dowork-in-hibernate.html
public static void getThroHibConnTest() throws Exception {
logger.debug("UsersActiion.getThroHibConnTest() | BEG ");
Transaction tx = null;
Connection conn = null;
CallableStatement cs = null;
Session session = HibernateUtil.getInstance().getCurrentSession();
try {
tx = session.beginTransaction();
conn = session.connection();
System.out.println("Connection = "+conn);
if (cs == null)
{
cs =
conn.prepareCall("{ ?=call P_TEST.FN_GETSUM(?,?) }");
}
cs.clearParameters();
cs.registerOutParameter(1,OracleTypes.INTEGER);
cs.setInt(2,1);
cs.setInt(3,2);
cs.execute();
int retInt=cs.getInt(1);
tx.commit();
}catch (Exception ex) {
logger.error("UsersActiion.getThroHibConnTest() | ERROR | " , ex);
if (tx != null && tx.isActive()) {
try {
// Second try catch as the rollback could fail as well
tx.rollback();
} catch (HibernateException e1) {
logger.debug("Error rolling back transaction");
}
// throw again the first exception
throw ex;
}
}finally{
try {
if (cs != null) {
cs.close();
cs = null;
}
if(conn!=null)conn.close();
} catch (Exception ex){;}
}
logger.debug("UsersActiion.getThroHibConnTest() | END ");
}

how to use multiple queries in java using jdbc

how to use multiple queries in java using jdbc
1.how to use this below query in the method without deleting the already existing query in
method
Insert into item_details(stock_name,temple,quantity)
SELECT a.stock_name, a.temple, SUM(Case when Type='purchase' then quantity else
(quantity*-1) End) AS quantity
FROM purchase_details a
GROUP BY a.stock_name, a.temple
public boolean insertIntimationDetails(StockForm ofform) {
boolean status=false;
PreparedStatement pst=null;
Connection conn=null;
try {
System.out.println("Inside insertIntimationDetails ");
String query=" update purchase_details set intimation_quantity = ? where
temple=? and Stock_name=? ";
System.out.println(query);
conn=getConnection();
System.out.println(query);
pst=conn.prepareStatement(query);
System.out.println(ofform.getIntimationQuantity());
pst.setString(2, ofform.getForTemple());
pst.setString(3, ofform.getStockName());
pst.setLong(1, ofform.getIntimationQuantity());
int rows= pst.executeUpdate();
if(rows>0){
status=true;
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(pst!=null)
pst.close();
if(conn!=null)
conn.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return status;
}
You can make the two SQLs atomic by using something similar to below code. This guarantees all or nothing rule.
public boolean insertIntimationDetails(StockForm ofform) {
boolean status = false;
PreparedStatement pst = null;
Connection conn = null;
Statement stat = null;
try {
System.out.println("Inside insertIntimationDetails ");
String query = " update purchase_details set intimation_quantity = ? where temple=? and Stock_name=? ";
System.out.println(query);
conn = getConnection();
conn.setAutoCommit(false); // Disable Auto Commit
System.out.println(query);
pst = conn.prepareStatement(query);
System.out.println(ofform.getIntimationQuantity());
pst.setString(2, ofform.getForTemple());
pst.setString(3, ofform.getStockName());
pst.setLong(1, ofform.getIntimationQuantity());
int rows = pst.executeUpdate();
if (rows > 0) {
status = true;
}
stat = conn.createStatement();
boolean status2 = stat
.execute("Insert into item_details(stock_name,temple,quantity) SELECT a.stock_name, a.temple, SUM(Case when Type='purchase' then quantity else (quantity*-1) End) AS quantity FROM purchase_details a GROUP BY a.stock_name, a.temple");
if (status && status2) {
conn.commit();
} else {
conn.rollback();
}
} catch (Exception e) {
e.printStackTrace();
conn.rollback();
} finally {
try {
if (pst != null)
pst.close();
if (stat != null)
stat.close();
if (conn != null)
conn.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return status;
}

jdbc get generatedKeys along with other data efficieintly

After batch insert a number of rows, I wish to retrieve the generated keys along with their corresponding inserted rows. how do I do this efficiently? Naively I can use statement.getGeneratedKeys() to query the database for each row based on each generated id, but that seems slow. the code below does a batch insert and then go through all the results in the table, however I don't want to include data that already exists in the table prior to insertion.
is there an alternative?
public static void main(String[] args) throws Exception {
Connection conn = getMySqlConnection();
ResultSet rs = null;
Statement stmt = null;
try {
conn = getMySqlConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
conn.setAutoCommit(false);
stmt.addBatch("INSERT INTO survey(id, name) VALUES('11', 'Alex')");
stmt.addBatch("INSERT INTO survey(id, name) VALUES('22', 'Mary')");
stmt.addBatch("INSERT INTO survey(id, name) VALUES('33', 'Bob')");
int[] updateCounts = stmt.executeBatch();
System.out.println(updateCounts);
conn.commit();
rs = stmt.executeQuery("SELECT * FROM survey");
while (rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
System.out.println("id="+id +" name="+name);
}
}
catch(BatchUpdateException b) {
System.err.println("SQLException: " + b.getMessage());
System.err.println("SQLState: " + b.getSQLState());
System.err.println("Message: " + b.getMessage());
System.err.println("Vendor error code: " + b.getErrorCode());
System.err.print("Update counts: ");
int [] updateCounts = b.getUpdateCounts();
for (int i = 0; i < updateCounts.length; i++) {
System.err.print(updateCounts[i] + " ");
}
}
catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
System.err.println("SQLState: " + ex.getSQLState());
System.err.println("Message: " + ex.getMessage());
System.err.println("Vendor error code: " + ex.getErrorCode());
}
catch(Exception e) {
e.printStackTrace();
System.err.println("Exception: " + e.getMessage());
}
finally {
rs.close();
stmt.close();
conn.close();
}
}
You have a list of IDs you are interested in. You can use the 'id in (...,...,)' constraint:
StringBuilder newIds = new StringBuilder();
ResultSet rs = stmt.getGeneratedKeys();
while (rs.next()) {
if (newIds.length() > 0) newIds.append(',');
newIds.append(rs.getInt(1));
}
if (newIds.length() > ) {
rs = stmt.executeQuery("SELECT * FROM survey where id in ("+newIds+")");
...
}

Resources