Java中遇到过的一些异常及处理

Posted hujingnb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java中遇到过的一些异常及处理相关的知识,希望对你有一定的参考价值。

异常:Bean named ‘org.springframework.transaction.interceptor.TransactionInterceptor#0‘ is expected to be of type ‘org.aopalliance.aop.Advice‘ but was actually of type ‘org.springframework.transaction.interceptor.TransactionInterceptor‘
场景;在使用spring整合hibernate事务时报错
解决:spring-aop中已经包含aopaliance,删除多余的jar包

 

异常:getHibernateFlushMode is not valid without active transaction; nested exception is org.hibernate.HibernateException: getHibernateFlushMode is not valid without active transaction getHibernateFlushMode is not valid without active transaction
场景:在使用spring整合hibernate调用的HibernateTemplate时报错
解决: 在spring配置文件中添加事务的配置

    <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <!-- 注入sessionFactory,配置sessionFactory -->
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager">
           <tx:attributes>
            <tx:method name="get*" read-only="true" />
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
           <aop:pointcut id="personServiceOperation" expression="execution(* user.service.UserServiceImpl.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="personServiceOperation" />
    </aop:config>

 

异常android.os.NetworkOnMainThreadException
场景:安卓开发时在主线程访问网络
解决:将访问网络的代码使用Thread操作

Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg){
        Bundle data = msg.getData();
        //从data中拿出存的数据
        String val = data.getString("value");
        //将数据进行显示到界面等操作
    }
};
Runnable runnable = new Runnable(){
    @Override
    public void run(){
        //进行访问网络操作
        Message msg = Message.obtain();
        Bundle data = new Bundle();
        data.putString("value", "存放数据");
        msg.setData(data);
        handler.sendMessage(msg);
    }
};

 


异常: Call From * 9000 failed on connection exception: java.net.ConnectException: Connection refused: no further information; For more details see: http://wiki.apache.org/hadoop/ConnectionRefused
场景: eclipse链接不上阿里云hadoop
解决: 将hadoop的配置文件中的ip改为内网IP即可

 

异常: Recieved SHUTDOWN signal from Resourcemanager ,Registration of NodeManager failed, Message from ResourceManager: NodeManager from localhost doesn‘t satisfy minimum allocations, Sending SHUTDOWN signal to the NodeManager.
场景: 在配置hadoop时,无法启动 NodeManager
解决: 本机配置不满足,修改yarn-siet.xml文件

<!-- 设置内存 -->
<property>
    <name>yarn.nodemanager.resource.memory-mb</name>
    <value>1600</value>
</property>
<!-- 设置cpu 核数 -->
<property>
    <name>yarn.nodemanager.resource.cpu-vcores</name>
    <value>1</value>
</property>

 


异常: The server time zone value ‘?D1ú±ê×?ê±??‘ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
场景: Java 连接 mysql 时连接不上
解决: 修改数据库时区
1.在mysql中执行如下语句
set global time_zone=‘+8:00‘;
2.修改数据库连接的url,加上如下参数(指定时区):
serverTimezone=GMT

 


 

 

可以关注一下鄙人的公众号, 谢谢各位了!技术分享图片



















以上是关于Java中遇到过的一些异常及处理的主要内容,如果未能解决你的问题,请参考以下文章

异常及throw与throws的介绍

JAVA基础——常见异常及错误

异常的捕获及处理

Java的异常处理

Java异常处理机制

异常及File类概述