I have a java application that runs as a cron. It uses MyBatis. In my mybatis-config.xml, I have
<environments default="staging">
<environment id="prod_read">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.10.2:3306/myDB"/>
<property name="username" value="dbuser"/>
<property name="password" value="dbpass"/>
</dataSource>
</environment>
<environment id="prod_write">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.10.1:3306/myDB"/>
<property name="username" value="dbuser"/>
<property name="password" value="dbpass"/>
</dataSource>
</environment>
<environment id="staging">.....
</environments>
So, I am using 2 DBs in production - one for read slave and one for write master.
In the code, the initialization of the SqlSessionFactory is like this
InputStream rd = this.getClass().getClassLoader()
.getResourceAsStream(mybatisConfigXml);
try {
sessionFactory_read = new SqlSessionFactoryBuilder().build(rd, Main.DB_ENV + "_read");
sessionFactory_write = new SqlSessionFactoryBuilder().build(rd, Main.DB_ENV + "_write");
}catch (final Exception e) {
e.printStackTrace();
}
When this code runs, I get an exception at the _write initialization with the message
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: java.io.IOException: Stream closed
How do I resolve this issue? Which stream is it talking about as being closed? Should I create 2 instances of InputStream (rd1, rd2) and use them in the 2 SqlSessionFactory initializations? I have checked the DB credentials and they are alright. Thanks in advance.
In this case you pass stream of mybatis configuration xml two times to
SqlSessionFactoryBuilder
. During the first invocation the stream is read fully and closed so during the second invocation you getstream is closed
exception.You need to open stream again.