java exception 异常错误记录

//异常:Could not obtain transaction-synchronized Session for current thread做定时器的时候用ApplicationContext的方法getBean("sessionFactory")获取sessionFactory;在获取getCurrentSession方法获取session,然后问题来了,直接报错: openSession每次打开都是新的Session,所以多次获取的Session实例是不同的,并且需要人为的调用close方法进行Session关闭。 getCurrentSession是从当前上下文中获取Session并且会绑定到当前线程,第一次调用时会创建一个Session实例,如果该Session未关闭,后续多次获取的是同一个Session实例;事务提交或者回滚时会自动关闭Sesison,无需人工关闭。 getCurrentSession方法的确和事务有关系所有要有事物。 而在spring的事务实现中需要判断当前线程中的事务是否同步,而没有事务的时候,那个判断是否同步的方法会因为get返回初始的null值而返回false,最终导致throw一个Could not obtain transaction-synchronized Session for current thread的异常.

异常:java.lang.Long cannot be cast to java.lang.String强制转换没有用括号把转换的包起来String sum = (String)query.setCacheable(true).list().get(0);//错String sum = (Long)(query.setCacheable(true).list().get(0))+"";//正确

婚姻犹如一艘雕刻的船,看你怎样去欣赏它,又怎样驾驭它。

java exception 异常错误记录

相关文章:

你感兴趣的文章:

标签云: