数据库重新启动后Jboss数据源恢复

Pra*_*nth 5 java jboss datasource

关闭连接仍在连接池中 - 为什么?

servlet-

public class Index extends HttpServlet {

    TimeZoneService timeZoneService;

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException {
        WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
        timeZoneService = (TimeZoneService) ctx.getBean("timeZoneService");
        timeZoneService.loadAllTimeZones();
        System.out.println("Done");
    }
}

public interface TimeZoneService {
    void loadAllTimeZones();
}

public class TimeZoneServiceImpl implements TimeZoneService {

    private TimeZoneDao tzDao;
    private Map<Long, String> tzOid2JavaName = new HashMap<Long, String>();

    public void loadAllTimeZones() {
        List<TimeZone> timeZones = tzDao.findAllTimeZones();
        for (TimeZone tz : timeZones) {
            tzOid2JavaName.put(tz.getOid(), tz.getJavaName());
        }
    }

    public void setTzDao(TimeZoneDao tzDao) {
        this.tzDao = tzDao;
    }
}

public interface TimeZoneDao {
    List<TimeZone> findAllTimeZones() throws DataAccessException;  
}

public class TimeZoneDaoImpl extends JdbcDaoSupport implements TimeZoneDao {

    public List<TimeZone> findAllTimeZones() throws DataAccessException
    {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT TZ.OID, TZ.JAVA_NAME FROM TIME_ZONE TZ");
        List<TimeZone> timeZones = getJdbcTemplate().query(sql.toString(), new RowMapper() {
            public Object mapRow(ResultSet rs, int i) throws SQLException {
                TimeZone tz = new TimeZone();
                tz.setOid(rs.getLong("OID"));
                tz.setJavaName(rs.getString("JAVA_NAME"));
                return tz;
            }
        });

        return timeZones;
    }
}

public class TimeZone {
    private Long oid;
    private String javaName;

    public Long getOid() {
        return this.oid;
    }

    public void setOid(Long oid) {
        this.oid = oid;
    }

    public String getJavaName() {
        return this.javaName;
    }

    public void setJavaName(String javaName) {
        this.javaName = javaName;
    }
}
Run Code Online (Sandbox Code Playgroud)

春天-config.xml中

<beans>

    <jee:jndi-lookup id="dataSource" jndi-name="java:/OracleDS"/>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="timeZoneDao" class="dao.impl.TimeZoneDaoImpl">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="timeZoneService" class="logic.impl.TimeZoneServiceImpl">
        <property name="tzDao" ref="timeZoneDao"/>
    </bean>

</beans>
Run Code Online (Sandbox Code Playgroud)

web.xml中

<web-app>

    <display-name>Spring</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            WEB-INF/spring-config.xml,classpath*:/META-INF/spring-config.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <servlet>
        <servlet-name>index</servlet-name>
        <display-name>Index page</display-name>
        <description>Landing page</description>
        <servlet-class>servlet.Index</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>index</servlet-name>
        <url-pattern>/index</url-pattern>
    </servlet-mapping>

    <!-- Session Timeout (in minutes) -->
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>
</web-app>
Run Code Online (Sandbox Code Playgroud)

的mysql-ds.xml文件

<datasources> 
   <local-tx-datasource> 
      <jndi-name>OracleDS</jndi-name> 
      <connection-url>jdbc:mysql://localhost:3306/spring</connection-url> 
      <driver-class>com.mysql.jdbc.Driver</driver-class> 
      <user-name>spring_test</user-name> 
      <password>spring_test13</password> 
      <min-pool-size>1</min-pool-size> 
      <max-pool-size>5</max-pool-size> 
      <idle-timeout-minutes>2</idle-timeout-minutes> 
   </local-tx-datasource> 
</datasources>
Run Code Online (Sandbox Code Playgroud)

Pra*_*nth 8

好.希望以下内容对某人有用:-)

有一个数据源配置设置 - exception-sorter-class-name

根据Jboss的说法,这是用来做的 a class that looks at vendor specific messages to determine whether sql errors are fatal and thus the connection should be destroyed. If none specified, no errors will be treated as fatal.

如果使用Oracle数据库,则此配置设置为org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter.此类具有需要被视为致命的所有错误代码,因此需要销毁连接.

在Jboss 4中,不包括错误代码17002(连接重置)&& 17008(连接关闭).它们是在Jboss 5中添加的.因此,如果您正在使用Jboss 4并想知道为什么连接没有恢复,请尝试添加丢失的代码.


ska*_*man 2

这是使用连接池时出现的常见问题。当应用程序从池中借用连接时,池本身是否应该“测试”该连接,以确保它仍然有效,还是应该将其留给应用程序?

如果池测试连接,这不可避免地涉及通过连接向数据库服务器发送某些内容(通常是某种基本的 SELECT)。在高流量系统上,这是极大的浪费,并且会给数据库服务器增加相当大的压力。

不过,在低流量站点上,您的数据库可以处理额外的负载,您可以配置数据源,让 JBoss 在将连接传递到应用程序之前验证连接。如果连接已失效,JBoss 会将其从池中删除并获取一个新连接,以便数据库重新启动后仍能继续存在。

任何,将其添加到您的mysql-ds.xml文件中:

<check-valid-connection-sql>select 1 from mytable</check-valid-connection-sql>
Run Code Online (Sandbox Code Playgroud)

您必须自己选择查询,确保它不是一个昂贵的查询,因为它会运行很多

请参阅JBoss 文档 wiki以了解如何修改这些数据源文件。