Navin,

Lifecycle of Spout and Bolt ensures that you can use fields which are
initialized in prepare() safely in execute(), nextTuple(), ack(), fail().
In other words, prepare() will be called earlier than other methods.
So please declare them as transient and initialize in prepare().

Hope this helps.

Thanks,
Jungtaek Lim (HeartSaVioR)

2016년 5월 4일 (수) 오후 6:48, Navin Ipe <navin....@searchlighthealth.com>님이 작성:

> Hi,
>
> I know that if a MySQL database connection is instantiated in the
> constructor of a Spout or Bolt, it won't work. It should be instantiated in
> open() or prepare().
>
> Problem is, when I store this database connection as a member of a class
> which is a member of a bolt. Eg:
>
>
>
>
>
>
>
>
>
>
>
>
>
> *public class MongoIteratorBolt extends BaseRichBolt {    private S1Table
> s1;}    public class S1Table implements Serializable {        private
> Connection connRef;    private Statement stmt;    private ResultSet rs;
>         public S1Table(Connection conn, final String tableName) {
> try {            this.connRef = conn;            this.stmt =
> conn.createStatement();            *
>
> I get an error like this:
>
>
>
>
>
>
>
>
>
> *    8811 [main] ERROR o.a.s.s.o.a.z.s.NIOServerCnxnFactory - Thread
> Thread[main,5,main] diedjava.lang.IllegalStateException: Bolt 'mongoBolt'
> contains a non-serializable field of type
> com.mysql.jdbc.SingleByteCharsetConverter, which was instantiated prior to
> topology creation. com.mysql.jdbc.SingleByteCharsetConverter should be
> instantiated within the prepare method of 'mongoBolt at the earliest.    at
> org.apache.storm.topology.TopologyBuilder.createTopology(TopologyBuilder.java:127)
> ~[MyStorm.jar:?]    at com.slh.Mystorm.MyStorm.main(MyStorm.java:76)
> ~[MyStorm.jar:?]Caused by: java.lang.RuntimeException:
> java.io.NotSerializableException:
> com.mysql.jdbc.SingleByteCharsetConverter    at
> org.apache.storm.utils.Utils.javaSerialize(Utils.java:167)
> ~[MyStorm.jar:?]    at
> org.apache.storm.topology.TopologyBuilder.createTopology(TopologyBuilder.java:122)
> ~[MyStorm.jar:?]    ... 1 moreCaused by: java.io.NotSerializableException:
> com.mysql.jdbc.SingleByteCharsetConverter    at
> java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
> ~[?:1.8.0_73]*
>
>
> I assume it is because of one of these which aren't getting serialized:
>
>
> *    private Connection connRef;    private Statement stmt;    private
> ResultSet rs;    *
>
> So if you can't declare them as class members because they don't get
> serialized, then how do you declare them so that the entire class will have
> access to it and I won't have to keep creating new connections for every
> query?
> I'm quite sure that declaring and initializing them in *prepare()* won't
> ensure that the rest of the class functions would be able to access it.
>
> --
> Regards,
> Navin
>

Reply via email to