Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
77 views
in Technique[技术] by (71.8m points)

java - Long native query getting frozen

I am using hibernate in my automation testing project, to execute a database 'clean-up routine' that is:

  • disabling constraints for all tables in the database
  • removing records by ID's that I stored when creating records used for my automation testing
  • enabling constraints for all tables in the database

Here is my pseudo code:

private SessionFactory sessionFactory;

private void initialize()
{
    try
    {
        Configuration config = createHibernateConfiguration();
        addAnnotatedClassesForClientDB(config);

        StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
        serviceRegistryBuilder.applySettings(config.getProperties());
        serviceRegistry = serviceRegistryBuilder.build();
        sessionFactory = config.buildSessionFactory(serviceRegistry);
    }
    catch (HibernateException e)
    {
        logger.error("Problem creating session factory!");
        e.printStackTrace();
    }
}

public Session openSession()
{
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    return session;
}

public void cleanClientDatabase()
{
    Session session = openSession();

    try
    {
        logger.info("Client DB cleaning started...");
        String combinedQuery = // her comes my SQL query

        Query query = session.createNativeQuery(combinedQuery);
        query.executeUpdate();

        closeSession(session);
    }
    catch (Exception e)
    {
        logger.error("Failed cleaning Client DB! " + e.getClass().getSimpleName());
        e.printStackTrace();

        session.getTransaction().rollback();
        session.close();
    }
}

Now from time to time, it sticks at query.executeUpdate(); in cleanClientDatabase() method and will hang there forever, until I manually kill transaction in Microsoft SQL Management studio by PID.

For some reason an exception is never thrown so I can't tell what is the error, I suspect some sort of lock, what can I do to avoid this issue and fix my code?

Thank you.

question from:https://stackoverflow.com/questions/65908522/long-native-query-getting-frozen

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...